Skip to content Skip to sidebar Skip to footer

Django Filter Liked Posts By User

Post model class Post(models.Model): likes = models.ManyToManyField(User, related_name='liked_by', blank=True) I am trying to query all posts that are liked by a particular us

Solution 1:

You can use the prefetch_related and perform query as the below one.

User.objects.prefetch_related('liked_by').get(pk=1).liked_by.all()

Solution 2:

You probably want to look at it the opposite way so go through the

User.objects.get(pk=1).liked_by.all()

Post a Comment for "Django Filter Liked Posts By User"