Changes between Initial Version and Version 1 of Ticket #22843


Ignore:
Timestamp:
Jun 15, 2014, 7:15:19 PM (10 years ago)
Author:
Russell Keith-Magee
Comment:

It isn't clear to me at all why this is the behaviour your would expect. No other Python object has an automatic and implied ordering on an arbitrary attribute, and there's nothing on the User model to say that "username" is the best attribute for this ordering (and I presume you meant "username", not "user"). The default User model doesn't have any predefined ordering defined. The order returned by the database is unpredictable (although, for small data sets, it will generally match the order of database insertion).

In theory, I suppose it would be possible for any object with an ordering defined to automatically gain a __cmp__ method that implements that search order. However, I'm not sure I agree that this sort of implicit behaviour would be desirable. It would also be complicated to implement in a way that doesn't override manually defined __cmp__ methods on a model.

It would also be possible to make a special case of the User object and manually define a __cmp__ method specifically for User; however, that would also act as an encouragement for people to do sorting in Django, rather than in the database (where the operation can be optimised).

So - marking this wontfix. If you want to argue for this change, please start a thread on django-developers.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #22843

    • Property Resolutionwontfix
    • Property Status newclosed
  • Ticket #22843 – Description

    initial v1  
    22
    33The reason can be seen in the following manage.py shell session:
     4{{{
    45>>> from django.contrib.auth.models import User
    56
     
    2122>>> u1 == u1_
    2223>>> True
    23 
     24}}}
    2425I think < compares using the object address while == uses the id attribute to compare.
    2526
    2627A more complex example that shows the failure is:
    27 
     28{{{
    2829>>> sum(1 for _ in groupby(map(attrgetter('user'), MyModel.objects.order_by('user'))))
    293016
    3031>>> sum(1 for _ in groupby(map(attrgetter('user'), sorted(MyModel.objects.all(), key=attrgetter('user')))))
    313290
    32 
     33}}}
    3334This means that in the first example there are 16 unique related Users in the MyModel objects. The second line finds considerably more Users because the sorting fails and so groupby doesn't detect duplicates.
    3435
Back to Top