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

  • SEARCH
  • Home
  • 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 6778773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:18:21+00:00 2026-05-26T16:18:21+00:00

I have a Video object with two ManyToMany fields. Each will contain a list

  • 0

I have a Video object with two ManyToMany fields. Each will contain a list of Users. I’m wondering how important it is to optimize so that there is only one db call when accessing a Video – or if there’s even a way to do that. Also – i’ll only be using the count of each of these ManyToMany fields – is it ‘cheaper’ to call Count() than to get the entire list then do ‘length’?.

For example:

class Video(models.Model):
  ...
  dislikes = models.ManyToManyField(User, null=True)
  likes = models.ManyToManyField(User, null=True)
  ...

In my template for rendering a particular video object – I need the count for each of likes and dislikes. What’s the performance optimal way to do that? It seems to me calling count() which results in two extra db calls is the best – but I’m not sure…

  • 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-05-26T16:18:21+00:00Added an answer on May 26, 2026 at 4:18 pm

    From the documentation:

    “Don’t use len() on QuerySets if all you want to do is determine the number of records in the set. It’s much more efficient to handle a count at the database level, using SQL’s SELECT COUNT(*), and Django provides a count() method for precisely this reason.”

    There is another way you may want to go, instead of using count() as a separate operation, you may want to annotate the QuerySet with the count of likes/dislikes. Something like:

    video = Video.objects.get(id=<video_id>).annotate(num_likes=Count('likes'), num_dislikes=Count('dislikes'))
    

    This will get a Video object with num_likes and num_dislikes fields. You could use filter() or all() instead of get() to get multiple videos, each with a num_likes and num_dislikes field. In your template, you would have {{ video.num_likes }} and {{ video.num_dislikes }} to show these two annotated fields.

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

Sidebar

Related Questions

I have one video object and 3 thumbnails which, when clicked, will play different
I have a flex 3 app that uses netstream and a video object to
I have a Video object that i set like below this._VideoInput = new Video(241,241);
I have the following piece of code that checks if the returned video object
I've got a simple object post that has two NSMutableArrays as properties. One is
I have two arrays which contain objects of assets, now I want to subtract
I have two tables, subscriptions and topics. Each subscription is related to a specific
I have an app that streams video from the net and plays it using
I'm trying to attach a single NetStream Object to two separate Video objects instead
I have a WMV file playing in an AxWindowsMediaPlayer object. I have two buttons

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.