简体   繁体   English

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

[英]type object 'Notification' has no attribute 'object'

I get this error from my views.py:我从我的 views.py 得到这个错误:

type object 'Notification' has no attribute 'object'类型对象“通知”没有属性“对象”

and my views.py:和我的意见.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()

and also my models.py:还有我的models.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)

I've been missing for a long time.我已经失踪很久了。 using this language.使用这种语言。 Then help me with this error.然后帮我解决这个错误。

You are trying to get notification by using Notification.object.get(id=notification.id) .您正在尝试使用Notification.object.get(id=notification.id)获取通知。

Replace object with objects in order to query notifications.替换objectobjects以查询通知。

for an instance, Notification.objects.get(id=notification.id)例如, Notification.objects.get(id=notification.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM