我有一个论坛模型。我有一个问题、正文、pub_date、图像、创建者、喜欢的字段。从管理员,我可以为特定论坛的给定用户添加喜欢。但是我现在很困惑,如何在视图中为 request.user 添加喜欢?
模型.py:
class Forum(models.Model):
question = models.CharField(max_length=150)
body = models.TextField()
pub_date = models.DateTimeField(default=datetime.now)
image = models.ImageField(upload_to=get_upload_file_name)
creator = models.ForeignKey(User, related_name="creator_set")
likes = models.ManyToManyField(User, through="Like")
def __unicode__(self):
return self.question
class Like(models.Model):
forum = models.ForeignKey(Forum)
liked = models.ForeignKey(User)
liked_date = models.DateTimeField(default=datetime.now)
def __unicode__(self):
return "%s likes %s" % (self.liked, self.forum)
我想过这样做:
>>>forum = Forum.objects.get(id=1)
>>>user = request.user
>>>user.like_set.add(forum)
但是,它没有帮助。请,任何帮助将不胜感激。谢谢你。