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 7780129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:49:02+00:00 2026-06-01T18:49:02+00:00

The title is probably confusing but I didn’t know how else to paraphrase it.

  • 0

The title is probably confusing but I didn’t know how else to paraphrase it. Here is an example.

Is it better to do something like this

class UserType( models.Model ) :
    def user_count( self ) :
        return self.userprofile_set.count()

or like this?

class UserType( models.Model ) :
    def user_count( self ) :
        # Does "user_types = self" work? I'm not sure.
        return UserProfile.filter( user_types = self.pk ).count()

where

class UserProfile( models.Model ) :
    user_types = models.ManyToManyField( UserType )

UserType.user_count() simply returns the number of UserProfile who has a particular UserType.

Sorry if this is a stupid, trivial question, but I was curious. Perhaps some Django expert can dig in on the actual SQL performance or something :-).

  • 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-01T18:49:03+00:00Added an answer on June 1, 2026 at 6:49 pm

    They are the same. Choose whatever you like.

    In my opinion the first looks cleaner. The second seems a bit incorrect because user_types is not a single pk value.

    By the way, it is recommended to always specify a related_name attribute.

    For example if it would be:

    user_types = models.ManyToManyField(UserType, related_name='profiles')
    

    and then:

    return self.profiles.count()
    

    Looks even cleaner.

    But both statements generate the same sql, we just could say that first is the shortcut for second. So it’s not about sql-performance

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

Sidebar

Related Questions

This sounds like a weird title and probably not stated too well. But here's
I've probably written the title incorrectly here, but anyway... When you create, for example,
the title probably is misleading, but i didn't really know how to name it.
So, this title probably makes me look new and like I haven't looked for
I think the title for this question is probably wrong, but I'm not sure
So, after this wonderful and probably impossible to understand title, here is my problem.
Probably a simple answer to this. In php, something like this would work fine.
This is probably something simple I'm overlooking but my google-fu isn't turning anything up
I'm probably misunderstanding JSON, but why this code doesn't work? HTML <html> <head> <title>Test</title>
This is probably a stupid question but I'll chance it anyways... I've got something

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.