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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:32:49+00:00 2026-05-24T01:32:49+00:00

I am trying to add an additional custom field to a django model. I

  • 0

I am trying to add an additional custom field to a django model. I have been having quite a hard time figuring out how to do the following, and I will be awarding a 150pt bounty for the first fully correct answer when it becomes available (after it is available — see as a reference Improving Python/django view code).

I have the following model, with a custom def that returns a video count for each user —

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    positions = models.ManyToManyField('Position', through ='PositionTimestamp', blank=True)

    def count(self):
        from django.db import connection
        cursor = connection.cursor()
        cursor.execute(
        """SELECT (
            SELECT COUNT(*)
                FROM videos_video v
                WHERE v.uploaded_by_id = p.id
                OR EXISTS (
                    SELECT NULL
                    FROM videos_videocredit c
                    WHERE c.video_id = v.id
                    AND c.profile_id = p.id
                )
            ) AS Total_credits
            FROM userprofile_userprofile p
            WHERE p.id = %d"""%(int(self.pk))
        )
        return int(cursor.fetchone()[0])

I want to be able to order by the count, i.e., UserProfile.objects.order_by('count'). Of course, I can’t do that, which is why I’m asking this question.

Previously, I tried adding a custom model Manager, but the problem with that was I also need to be able to filter by various criteria of the UserProfile model: Specifically, I need to be able to do: UserProfile.objects.filter(positions=x).order_by('count'). In addition, I need to stay in the ORM (cannot have a raw sql output) and I do not want to put the filtering logic into the SQL, because there are various filters, and would require several statements.

How exactly would I do this? 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-24T01:32:50+00:00Added an answer on May 24, 2026 at 1:32 am

    “couldn’t you use something like the “extra” queryset modifier?”

    see the docs

    I didn’t put this in an answer at first because I wasn’t sure it would actually work or if it was what you needed – it was more like a nudge in the (hopefully) right direction.

    in the docs on that page there is an example

    query

    Blog.objects.extra(
        select={
            'entry_count': 'SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id'
        },
    )
    

    resulting sql

    SELECT blog_blog.*, (SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id) AS entry_count
    FROM blog_blog;
    

    Perhaps doing something like that and accessing the user id which you currently have as p.id as appname_userprofile.id

    note:

    Im just winging it so try to play around a bit.
    perhaps use the shell to output the query as sql and see what you are getting.

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

Sidebar

Related Questions

I am trying to create a custom profile to add additional fields to django-registration
I'm trying to add an additional cell for custom content as the last cell
I'm trying to implement PayPal on a completely custom shopping cart and have been
I am trying to add some additional logic to the 'Promoted to front page'
I'm trying to add some additional key/value pairs to an NSMutableDictionary, using: Tag *tag1
Trying to add a 'box' to a form at design time, I looked up
I am trying to add some additional html to a div with id slideshow
I am trying to override a the redirect_to method to add an additional param
We have a custom namespace we have for custom elements: <rss xmlns:blah=http://example.com/> I'm trying
I'm having trouble trying to properly insert additional code into the onclick attribute of

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.