类型对象“通知”没有属性“对象”

时间:2019-04-02 14:21:45

标签: python django

我从view.py中收到此错误

  

类型对象'Notification'没有属性'object'

和我的view.py

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from notification.models import Notification

def show_notification(request, notification_id):
    n = Notification.object.get(id=notification_id)

    return render_to_response('notification.html', {'notification':n})
def delete_notification(request, notification_id):
    n = Notification.object.get(id=notification_id)
    n.viewed = True
    n.save()

还有我的模型。py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver


class Notification(models.Model):
    title = models.CharField(max_length=250)
    message = models.TextField()
    viewed = models.BooleanField(default=False)
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING)


def create_welcome_message(sender, **kwargs):
    if kwargs['created']:
        noti=Notification.objects.create(user=kwargs['instance'],
                                    title="Welcome Message",
                                    message="Thank you for singing up!")


post_save.connect(create_welcome_message, sender=User)

我已经失踪很久了。使用这种语言。然后帮我解决这个错误

1 个答案:

答案 0 :(得分:1)

您正尝试使用Notification.object.get(id=notification.id)来获取通知。

object替换objects以便查询通知。

对于实例,Notification.objects.get(id=notification.id)