3

私は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)だけでなく、データベースで更新操作も行いたいです。どうやってするの?前もって感謝します。

4

1 に答える 1

2

必要なものを理解しているかどうかわかりません。私があなたのコードを理解しているように、status.replies.all().update(has_read=True)変更はありませんstatusが、返信のみが変更されます。それが本当なら、コードはあなたが望むことをするはずです。そうでない場合は、次のコピーを作成しstatusて返すことができます。

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

それとも、メソッドを早く返して、データベースを非同期で更新したいだけですか? 次に、セロリと、おそらくこの素晴らしい説明を見てください。

于 2011-06-07T12:46:25.457 に答える