【问题标题】:Django 3.0: django-notifications giving error: "Notification.recipient" must be a "User" instanceDjango 3.0:django-notifications 给出错误:“Notification.recipient”必须是“User”实例
【发布时间】: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


    【解决方案1】:

    这是一个超级简单的解决方案。你已经成功了 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。

    【讨论】:

    • 我去试试你的建议,但我得到了这个Field 'id' expected a number but got '<property object at 0x0000020F13765B88>'。我不知道我做了什么它不允许我通过django admin 打开/admin/notifications/notification/。通过管理员我得到了同样的错误。
    • 我正在重新阅读文档,您似乎应该提供用户组、用户查询集或用户列表。也许改用recipient=[book.seller.user, ]?您似乎也可以尝试notify.send(book.seller.user, recipient=book.seller.user...),但他们的文档有点混乱。 github.com/django-notifications/…
    • 是的,但这个错误Field 'id' expected a number but got '<property object at 0x0000020F13765B88> 不会消失。我已经注释掉了所有与notifications 相关的代码,甚至是卸载的应用程序。然后我再次安装了它,但这个错误并没有通过 django admin 在 /admin/notifications/notification/ 消失。帮助我如何从db 中删除此表并重新安装。
    • 我的db 回来了。但是我的查询的解决方案呢?你想清楚了吗?
    • 太棒了!抱歉,我们第一次没有做到 100% 正确。
    猜你喜欢
    • 2019-03-21
    • 2015-01-05
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    相关资源
    最近更新 更多