title: Django: Order a model by a many-to-many field
body: <p>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.</p>
  
  <p>My initial idea was to add a denormalized field called <em>heaviest-role-weight</em> - 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 (<a href="http://code.djangoproject.com/ticket/5390" rel="noreferrer">yet</a>, anyway).</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Has anyone done this before - or have any ideas how it could be achieved?</p>