0

RelatedModel.new_reply在将新值保存到数据库中的该字段后,我试图显示数据库模型字段 ( ) 的旧值。这应该起作用的方式是该new_reply字段将回复(通过将其设置为 1)标记为收件人未读(rto)。收件人查看邮件后,new_reply将重置为 0。如果收件人有尚未单击查看的新回复,模板 my_view.html 会向收件人显示一个标志。收件人第一次查看回复时,该标志new_reply被重置,后续视图不会被标记为新的。

问题在于,除了存储在数据库中的值之外,该行response.save()似乎还更改了new_replyin的值。d因此,my_view.html 永远不会看到new_reply==1,只有new_reply==0. 这对我来说似乎很奇怪,因为d一旦分配它就应该独立于数据库。

任何想法为什么会发生这种情况以及如何解决它?

class ParentModel (models.Model):
    msg = models.TextField()
    author = models.ForeignKey(User)

class RelatedModel (models.Model):
    msg = models.ForeignKey(ParentModel)
    reply = models.TextField()
    rto = models.ForeignKey(User, related_name='rto')
    new_reply = models.IntegerField(null=True, blank=True)

def view_msg(request, msg_id):
    m = get_object_or_404(ParentModel, pk=id)
    r = m.RelatedModel_set.all()
    d = add_csrf(request, m=m, r=r) # d.r.new_reply==1 should be rendered
    # Once the view data is ready for rendering, clear the new_reply
    # bit.  If we do it earlier, we will not be able to mark replies as new.
    if request.user.is_authenticated():
        for response in r:
            # Clear bit if current user is viewing reply addressed to them
            if (request.user == RelatedModel.rto):
                if response.new_reply == 1: # Is it a new reply?
                    response.new_reply = 0 # Reset the new_activity bits
                    response.save()
    return render_to_response('my_view.html',
                              {'db_data': d},
                              context_instance = RequestContext(request),
    )
4

0 回答 0