#20105: dictsort by obj and by obj_id gives varying results
-------------------------------------+-------------------------------------
     Reporter:  apollovy@…           |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Template system      |                  Version:  1.5
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
  dictsort,sorting,grouping,regroup,templatetag|  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by bmispelon):

 * cc: bmispelon@… (added)
 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Comment:

 Hi,

 `MyModel.obj_id` will give you back an integer so sorting a list according
 to this attribute is straightforward and does what you expect.

 However, `MyModel.obj` will give you back an instance of `SomeOtherModel`,
 which causes the behaviour you're describing, because comparing two model
 instances is somehow undefined (technically, it's very well defined but it
 most likely never does what you want).

 The technical reason behind that is that django's models don't implement a
 `__cmp__` method so when comparing them, python falls back to comparing
 their `id` (`id` being the python builtin function, not the primary key of
 the model) which is not consistent.

 See `http://docs.python.org/2/reference/datamodel.html#object.__cmp__` for
 more details.

 Note that this is not applicable to python3 where comparing two model
 instances together will fail with a `TypeError`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/20105#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to