[Fixed]-Django ORM extra info in query

1๐Ÿ‘

I would not separate these two models. Just add new field liked_by to Post Model

class Post(models.Model):
   user = models.ForeignKey(User, related_name='user_posts')
   liked_by = models.ManyToMany(User, related_names='liked_posts')

and

user_liked_posts = request.user.liked_posts.all()

If you cannot change models, then:

user_liked_posts = request.user.post_likes.select_related('post')
# likes on posts done by request.user
# so "author_user" is request.user  
for like in user_liked_posts:
    like.dateJoined
    like.post.title 
๐Ÿ‘คdoniyor

Leave a comment