Trying to use add_notification() method on UserProfile object, ValueError: cannot assign UserProfile - Notification.user must be a User instance

Trying to use add_notification() method on UserProfile object, ValueError: cannot assign UserProfile - Notification.user must be a User instance

我正在尝试在我的 Django 应用程序中使用默认用户模型。我创建了 UserProfile 个对象,为用户提供 custom/additional 个字段。我正在尝试将 Notification 对象分配给每个 UserProfile,并且我有以下代码块

allUsers = User.objects.all()
for each in allUsers:
    uprof = UserProfile.objects.get_or_create(user=each)

for u in allUsers:
       if u.userprofile:
           notif1 = u.userprofile.add_notification(title="Welcome to our site " + u.email, body="Your first notification") # error
           notif2 = u.userprofile.add_notification(title="Sample Notification" + u.email, body="Empty template for " + u.email) # also same error

我在 django shell 加上 运行 这个。第一个 for 循环遍历所有用户并为他们提供一个 UserProfile 对象。第二个尝试使用名为 add_notification() 的用户配置文件方法向该用户分配通知。这会失败并生成错误

ValueError: Cannot assign "<UserProfile: devtest4@gmail.com>": "Notification.user" must be a "User" instance.

我真的不知道这条错误信息是什么意思。即便如此,我认为这是为每个现有用户分配 UserProfile 然后向每个用户各自的用户配置文件添加通知的正确方法。我这样做错了吗?

user_profile/models.py

class UserProfile(models.Model):
    phone_number    = models.CharField(max_length=15, verbose_name='Phone Number')
    user            = models.OneToOneField(User, on_delete = models.CASCADE)
    api_key         = models.CharField(max_length=200, default='12345678')
    class Meta:
           verbose_name = "User Profile"
           verbose_name_plural = "User Profile"
    def __str__(self):
        return str(self.user.email)

    def add_notification(self, title, body):
        notif = Notification(user=self.user, title=title, body=body)
        notif.save()

notifications/models.py

class Notification(models.Model):

    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('receiver'), related_name='notifications_receiver')
    title = models.CharField(_('title'))
    body = models.TextField(_('text'), blank=True)
    timestamp = models.DateTimeField(_('timestamp'), auto_now_add=True)
    is_seen = models.BooleanField(_('seen status'), default=False)
    is_read = models.BooleanField(_('read status'), default=False)

如您在错误中所见:

ValueError: Cannot assign "<UserProfile: devtest4@gmail.com>": "Notification.user" must be a "User" instance. 

您正在尝试将 UserProfile 实例分配给此处应为 User 实例的字段:

notif1 = u.userprofile.add_notification(title="Welcome to our site " + u.email, 
                                        body="Your first notification") 

所以不是这个:

for u in allUsers:
       if u.userprofile:
           notif1 = u.userprofile.add_notification(title="Welcome to our site " + u.email, 
                                                   body="Your first notification")
           notif2 = u.userprofile.add_notification(title="Sample Notification" + u.email, 
                                                  body="Empty template for " + u.email) 

这样做:

for u in allUsers:
       if u.userprofile:
           notif1 = u.userprofile.add_notification(user=u, 
                                                   title="Welcome to our site " + u.email, 
                                                   body="Your first notification")
           notif2 = u.userprofile.add_notification(user=u, 
                                                   title="Sample Notification" + u.email, 
                                                   body="Empty template for " + u.email)

并像这样重写你的方法:

def add_notification(self, user, title, body):
    notif = Notification(user=user, title=title, body=body)
    notif.save()