Django 3.0: django-notifications giving error: "Notification.recipient" must be a "User" instance
Django 3.0: django-notifications giving error: "Notification.recipient" must be a "User" instance
我正在使用 django-notifications-hq 包 link of django-notifications-hq 并收到错误:
Cannot assign "<Seller: john@gmail.com>":
"Notification.recipient" must be a "User" instance.
我的自定义 user
型号:
class User(AbstractBaseUser):
email = models.EmailField(max_length=255, unique=True)
name = models.CharField(max_length=255, blank=True, null=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
customer = models.BooleanField(default=False)
seller = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
我的 seller
模型与 user
具有一对一关系:
class Seller(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
seller_picture = models.ImageField(upload_to='profile_pic/seller', null=True, blank=True)
我的 Book
模型有外键 seller
:
class Book(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField('Title', max_length=255)
authors = models.ManyToManyField(Author, related_name='book_written_by')
seller = models.ForeignKey(Seller, on_delete=models.CASCADE)
price = models.DecimalField('Price', decimal_places=2, max_digits=10)
description = models.TextField('Description')
在我 views
的某处成功获得 books
后,我想通知所有相应的 sellers
有人已经买了他们的书:
for book in books:
notify.send(Seller, recipient=book.seller, verb="User has just bought book named {} priced
as {}".format(book.title, book.price))
我知道它工作正常 recipient = request.user
但我有不同的情况。我怎样才能使 book.seller
成为 User
实例?
这是一个超级简单的解决方案。您已完成 99%!
而不是这个:
for book in books:
notify.send(Seller, recipient=book.seller, verb="User has just bought book named {} priced
as {}".format(book.title, book.price))
试试这个:
for book in books:
notify.send(book.seller.user, recipient=book.seller.user, verb="User has just bought book named {} priced
as {}".format(book.title, book.price))
Seller
有一个名为 user
的 属性,但它本身不是 user
。
我正在使用 django-notifications-hq 包 link of django-notifications-hq 并收到错误:
Cannot assign "<Seller: john@gmail.com>": "Notification.recipient" must be a "User" instance.
我的自定义 user
型号:
class User(AbstractBaseUser):
email = models.EmailField(max_length=255, unique=True)
name = models.CharField(max_length=255, blank=True, null=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
customer = models.BooleanField(default=False)
seller = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
我的 seller
模型与 user
具有一对一关系:
class Seller(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
seller_picture = models.ImageField(upload_to='profile_pic/seller', null=True, blank=True)
我的 Book
模型有外键 seller
:
class Book(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField('Title', max_length=255)
authors = models.ManyToManyField(Author, related_name='book_written_by')
seller = models.ForeignKey(Seller, on_delete=models.CASCADE)
price = models.DecimalField('Price', decimal_places=2, max_digits=10)
description = models.TextField('Description')
在我 views
的某处成功获得 books
后,我想通知所有相应的 sellers
有人已经买了他们的书:
for book in books:
notify.send(Seller, recipient=book.seller, verb="User has just bought book named {} priced
as {}".format(book.title, book.price))
我知道它工作正常 recipient = request.user
但我有不同的情况。我怎样才能使 book.seller
成为 User
实例?
这是一个超级简单的解决方案。您已完成 99%!
而不是这个:
for book in books:
notify.send(Seller, recipient=book.seller, verb="User has just bought book named {} priced
as {}".format(book.title, book.price))
试试这个:
for book in books:
notify.send(book.seller.user, recipient=book.seller.user, verb="User has just bought book named {} priced
as {}".format(book.title, book.price))
Seller
有一个名为 user
的 属性,但它本身不是 user
。