2017-10-16 71 views
0

模型字段的值我有这样的模式:更改通过视图

class UserNotification(models.Model): 
    user = models.ForeignKey(User,related_name='user',null=True) 
    post = models.ForeignKey('feed.UserPost',related_name='post') 
    timestamp = models.DateTimeField(auto_now_add=True) 
    notify_type = models.CharField(max_length=6) 
    read = models.BooleanField(default=False) 

    def get_absolute_url(self): 
     return reverse('notify:user_notifications') 

    def __str__(self): 
     return str(self.user) 

它记录用户的动作在关于其他用户的信息,这样的帖子的主人可以得到通知。

我再有这样的观点:

class NotifyMarkRead(RedirectView): 
    def get_redirect_url(self,pk): 
     obj = get_object_or_404(UserNotification,pk=pk) 
     if obj.read != True: 
      obj.read == True 
     else: 
      obj.read == False 
     return obj.get_absolute_url() 

这是处理通知上的用户点击美景。此视图应检查read是否等于True或False(默认为false)。如果用户点击通知,则视图应该将read更新为True。但是,这不是那样做的。那么当用户浏览这个视图时,如何更新模型中的read字段?

此外,我不是该网页正在访问,但它只是去/notify/1/read/而不是重定向回/notify/。请确定这是否重要。

这里是我的网址:

from django.conf.urls import url 
from notify import views 

app_name = 'notify' 

urlpatterns = [ 
    url(r'^$',views.UserNotifications.as_view(),name='user_notifications'), 
    url(r'^(?P<pk>\d+)/read/$',views.NotifyMarkRead.as_view(),name='user_notify_toggle'), 
] 

回答

2

obj.save()

试试这个保存更新

+0

我补充说,它仍然无法正常工作。 – Garrett

+3

您还在使用==进行分配。这是不正确的,使用single =而不是'obj.read == True'使用'obj.read = True' –

+0

不知道。这解决了,谢谢。 – Garrett