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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:57:26+00:00 2026-05-11T18:57:26+00:00

I am writing a Django application that has a model for People, and I

  • 0

I am writing a Django application that has a model for People, and I have hit a snag. I am assigning Role objects to people using a Many-To-Many relationship – where Roles have a name and a weight. I wish to order my list of people by their heaviest role’s weight. If I do People.objects.order_by(‘-roles__weight’), then I get duplicates when people have multiple roles assigned to them.

My initial idea was to add a denormalized field called heaviest-role-weight – and sort by that. This could then be updated every time a new role was added or removed from a user. However, it turns out that there is no way to perform a custom action every time a ManyToManyField is updated in Django (yet, anyway).

So, I thought I could then go completely overboard and write a custom field, descriptor and manager to handle this – but that seems extremely difficult when the ManyRelatedManager is created dynamically for a ManyToManyField.

I have been trying to come up with some clever SQL that could do this for me – I’m sure it’s possible with a subquery (or a few), but I’d be worried about it not being compatible will all the database backends Django supports.

Has anyone done this before – or have any ideas how it could be achieved?

  • 1 1 Answer
  • 1 View
  • 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-11T18:57:26+00:00Added an answer on May 11, 2026 at 6:57 pm

    Django 1.1 (currently beta) adds aggregation support. Your query can be done with something like:

    from django.db.models import Max
    People.objects.annotate(max_weight=Max('roles__weight')).order_by('-max_weight')
    

    This sorts people by their heaviest roles, without returning duplicates.

    The generated query is:

    SELECT people.id, people.name, MAX(role.weight) AS max_weight
    FROM people LEFT OUTER JOIN people_roles ON (people.id = people_roles.people_id)
                LEFT OUTER JOIN role ON (people_roles.role_id = role.id)
    GROUP BY people.id, people.name
    ORDER BY max_weight DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Django application that is using pip & virtualenv to manage its
I am writing a web application using Django, on djangorecipe. I wanted to have
I'm writing an application in Django and using several TimeFields in a model. I
I have an existing Django application with a pretty typical model implementation that's backed
I am writing a GUI application using Django 1.1.1. This is the views.py: from
I am looking for a basic Django application that looks good and has basic
I'm writing tests for a Django application that uses an external data source .
I'm writing a Django project that uses the LESS language. I'm using the django-css
I'm currently writing a small python app that embeds cherrypy and django using py2app.
I am writing a Python/Django web application that uses OAuth (for the TwitterAPI, not

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.