python - 类型对象“通知”没有属性“对象”
问题描述
我从views.py 中得到这个错误:
类型对象“通知”没有属性“对象”
和我的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)
推荐阅读
- javascript - Vue 3 - 是否可以在不修改外部类的情况下使外部类的属性具有反应性
- api - 如何遍历数组并在 shell 中使用命令中的值?
- python - 如何将购物车创建限制为(特定网址)“/api/cart/”网址?
- python - 预测结果 - TensorFlow
- wikipedia - 从 wikipedia api 获取电影的文章文本?
- python - PyTorch 中的多个内核大小?
- python - Forloop 正在改变列表的类型
- reactjs - 单页应用的在线用户数和应用访问量
- bash - 删除目录中除一个文件之外的所有内容 - 使用 SSH
- tree - 如何使用在线工具构建图形或树结构