How to update a model, but return an unmodified model to Django?

I am using django-piston to write RESTful web service and problems.

in models.py:

class Status(models.Model):
    user = models.ForeignKey(User)
    content = models.TextField(max_length=140)

class StatusReply(models.Model):
    user = models.ForeignKey(User)
    reply_to = models.ForeignKey(Status, related_name='replies')
    content = models.TextField(max_length=140)
    has_read = models.BooleanField(default=False, help_text="has the publisher of the status read the reply")

in handlers.py:

class StatusHandler(BaseHandler):
    allowed_methods = ('GET', 'POST', 'DELETE' )
    model = Status
    fields = ('id', 
              ('user', ('id', 'username', 'name')), 
              'content', 
              ('replies', ('id', 
                           ('user', ('id', 'username', 'name')), 
                           'content',  
                           'has_read'),
              ),
             )

    @need_login
    def read(self, request, id, current_user): # the current_user arg is an instance of user created in @need_login
        try:
            status = Status.objects.get(pk=id)
        except ObjectDoesNotExist:
            return rc.NOT_FOUND
        else:
            if status.user == current_user: #if current_user is the publisher of the status, set all replies read
                status.replies.all().update(has_read=True)
            return status

In the handler, it returns a specific status by id. Now I want to return the status to status.replies.all().update(has_read=True), and also perform an update operation in the database. How to do it? Thanks in advance.

+3
source share
1 answer

Not sure I understand what you need. As far as I understand your code, it status.replies.all().update(has_read=True)does not change status, but only changes the answers. If true, the code should do what you want. If this is not the case, you can make a copy statusand return the copy:

        if status.user == current_user: 
            old_status = status.make_copy()
            status.replies.all().update(has_read=True)
            return old_status
        return status

, ? celery , , .

+2

All Articles