我正在使用django-piston编写一个 RESTful Web 服务并遇到问题。
在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")
在 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
在处理程序中,它通过 id 返回特定状态。现在我想返回之前的状态,status.replies.all().update(has_read=True)
还要在数据库中进行更新操作。怎么做?提前致谢。