【发布时间】:2021-03-26 07:58:25
【问题描述】:
我正在尝试在我的 django 应用程序中使用默认用户模型。我创建了UserProfile 对象来为用户提供自定义/附加字段。我正在尝试将 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 plus 中运行它。第一个 for 循环遍历所有用户并给他们一个 UserProfile 对象。第二个尝试使用名为add_notification() 的用户配置文件方法向该用户分配通知。这会失败并产生错误
ValueError: Cannot assign "<UserProfile: devtest4@gmail.com>": "Notification.user" must be a "User" instance.
我有点不知道这个错误信息是什么意思。即便如此,我认为这将是为每个现有用户分配用户配置文件然后向每个用户各自的用户配置文件添加通知的正确方法。我是不是搞错了?
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()
通知/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)
【问题讨论】:
标签: python django django-users