【发布时间】:2020-12-01 15:00:12
【问题描述】:
我正在使用 django-notifications-hq 包 link of django-notifications-hq 并收到错误:
无法分配“”: “Notification.recipient”必须是“用户”实例。
我的自定义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 具有1to1 关系:
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 实例?
【问题讨论】:
标签: django django-views pypi django-notification