Skip to content
Advertisement

How to get the most liked users on a particular date in django

So I have a social media app, where users can like the posts of other users. Now I fetch the top 20 users who have received the most number of likes. Everything is perfect. But the problem is I cant figure out , how I can fetch the top users who have received the most likes on a particular date, for example get the top users who received most likes only today

My LIKES MODEL

SIMPLIFIED POST MODEL

SIMPLIFIED USER MODEL

** My query to get the top users who received the most number of likes **

** My try to get the top users with most likes on a particular day**

But with the above query , I cant achieve it. I am getting the error: Related Field got invalid lookup: liked_on

I am pretty sure, I am doing something wrong with the many-many fields.

Advertisement

Answer

Q(author__likes__liked_on = datetime.today()) won’t work, because liked_on is a datetime, while datetime.today() is a date. And the filtered field is on the ‘through’ table.

So you need to cast liked_on to a date, and look up the field on postlike (lower-cased by default):

Q(author__postlike__liked_on__date = datetime.today()))

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement