how to add likes of an object for the given user
I have a model for forum. There I have a field for question, body,
pub_date, image, creator, likes. From the admin, I can add the likes for a
given user for a particular forum. But i'm confused all now, how to add
likes for the request.user in the views?
models.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)
Any help will be appreciated. Thank you.
No comments:
Post a Comment