[Django]-Django Models for user, like, page like situation

3👍

user = User.objects.get(username="foo")
user_likes = user.likes_set.all()

page = Page.objects.get(id=4)
page_likes = page.likes_set.all()

You can change the default name of the related manager in the definition of the fields in your Like model:

user = models.ForeignKey(User, related_name="likes")
page = models.ForeignKey(Page, related_name="likes")

And you can then call them like:

user_likes = user.likes.all()
page_likes = page.likes.all()

As a side note, there are a few Django apps out there that could do that job:

👤AJJ

Leave a comment