I would like to know how to frame the following to the Django ORM form:
SELECT name,mobile_number,COUNT(*) as count, created FROM core_user,core_useractivity WHERE core_user.id = core_useractivity.user_id GROUP BY user_id ORDER BY count DESC;
where name, mobile number are columns in core_user table or USER model, and created is a column in core_useractivity its model is UserActivity . The two tables are joined on user_id. Further I am grouping the rows by user_id displaying the result in Descending order.
Try annotating the users with the user activity count.
This will return a queryset of users, each with an attribute
num_activities. See the docs on annotation for more information.