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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:56:38+00:00 2026-05-23T02:56:38+00:00

I have the following models — class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) … class

  • 0

I have the following models —

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    ...

class Video(models.Model):
    title = models.CharField(max_length=256)
    uploaded_by = models.ForeignKey('UserProfile')
    ...

I would like to add a COUNT for the number of videos a user has.

Currently, to get the count of a user, I can do profile.video_set.count().

I would like to have an additional column in the UserProfile model so I can just do UserProfile.objects.order_by('video_count'). How would I add this custom row to the model? Thank you.

  • 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-23T02:56:38+00:00Added an answer on May 23, 2026 at 2:56 am

    What you are trying to do does not follow database normalization rules. The way to go is by using an annotation:

    UserProfile.objects.annotate(video_count=Count('video')).order_by('-video_count')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models defined: class Team(models.Model): Name = models.CharField(max_length=250) class Fixture(models.Model): HomeTeam
Let's say I have the following models: Class Wishlist belongs_to :user # User class
I have the following models: Users (id) Project (id, name) Roles (id, title) Permissions
I have the following models in my ASP.NET MVC 3 project: public class Task
I have the following models defined in my application User Profile Address Here is
In my rails app I have the following models: Messages (id, title, content, project_id)
I have the following models & mappings (code snippets further below). One Competition has
I have the following models: Game and Pick. There's a one to many association
In my app I have the following models: Users Groups Permissions (user_id, group_id, role_id)
I use mongoose in my node.js app, and basically have the following models: //

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.