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

标签 python django

我从我的 views.py 中得到这个错误:

type object 'Notification' has no attribute 'object'

和我的views.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()

还有我的 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)

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

最佳答案

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

object替换为objects,以便查询通知。

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

关于python - 类型对象 'Notification' 没有属性 'object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55477144/

相关文章:

django - 具有 'ignore case' 的唯一约束

python - 如何测试 POST 请求的 URL 参数?

javascript - Angular URL 标记

python - 如何根据其他列中的多个条件更新列数据?

python - 通过处理 500 处理程序中的一些错误并在这些情况下呈现正常的 HTTP 响应来明智地利用 ATOMIC_REQUESTS

python - OS X AppEngine - 导入错误 : No module named _sqlite3

python - 电子邮件上带有 unique_together 的 Django 自定义用户模型

python - Getpass 输入 Python 3

python - 如何使用在 Apache Spark 上训练的 ML 模型开发 REST API?

Django Rest Serializer.data 是一个空的 OrderedDict()