Django 3.0:django-notifications给出错误:“ Notification.recipient”必须是“ User”实例

问题描述

我正在使用django-notifications-hq软件包link of django-notifications-hq并收到错误消息:

无法分配“ ”: “ Notification.recipient”必须是“ User”实例。

我的自定义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实例?

解决方法

这是一个超级简单的解决方案。您已经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,book.price))

Seller具有一个名为user的属性,但它本身不是user

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...