Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9054987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:49:54+00:00 2026-06-16T13:49:54+00:00

Given this somewhat simplified representation of my application’s models, my question is how do

  • 0

Given this somewhat simplified representation of my application’s models, my question is how do I globally find the most popular MyModel? I.e., those MyModels are favorited the most by MyUsers.

I’ve come across similar blog posts about how to find favorite tags, but I don’t think those apply to this particular situation.

class MyUser(models.Model):
  favorite_models = models.ManyToManyField(MyModel)
  ...


class MyModel(models.Model):
  name = models.CharField(...)
  ...

Can this be done in a single query? Or do I need to loop over every MyUser and MyModel to determine the most popular? Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:49:55+00:00Added an answer on June 16, 2026 at 1:49 pm

    I’m too lazy to create a django project from scratch, but this one should do the job:

    from django.db.models import Count
    MyModel.objects.annotate(Count('myuser'))
    

    (or this)

    MyModel.objects.annotate(Count('myuser_set'))
    

    if not, try this:

    class MyUser(models.Model):
        favorite_models = models.ManyToManyField(MyModel, related_name='myuser')
    

    and then

    MyModel.objects.annotate(Count('myuser_set'))
    

    (let me know if it works, in any case this page should contain what you need to do that: https://docs.djangoproject.com/en/dev/topics/db/aggregation/)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this simplified snippet : <html> <body> <div> <div style='text-align:center;'>asdfaskjdfakjsd</div> <div style='float:right'> <input type='submit'
Problem Statement is somewhat like this: Given a website, we have to classify it
This is somewhat related to this question : I have a table with a
This is somewhat the second part to this question . In my project, I
While this question is somewhat language agnostic ( agnostic as far as OOP languages
I guess this question is somewhat wide but I'm trying to create a C
This is somewhat of a follow-up question to this question . Suppose I have
I apologize if this might be a somewhat similar question to others out there,
this is somewhat the same question as I've asked some time ago: How to
Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.