[英]type object 'Notification' has no attribute 'object'
我從我的 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()
還有我的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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.