tags:

views:

54

answers:

1

I set up an ordering='ordering_number' Meta attribute to my Django model, thinking that Django will use it when comparing instances. (ordering_number is an IntegerField in my model.)

For example, if I have an instance a with ordering_number = 4 and an instance b with ordering_number = 7, I'd expect that a < b would be True. However, I tested it, and it didn't seem to work. I did not understand according to which logic a < b would come out as True.

Does anyone know? Why doesn't Django uses ordering for element comparisons?

+2  A: 

From the documentation:

The default ordering for the object, for use when obtaining lists of objects

So the reason your comparisons aren't working is because they're not designed that way. Define __lt__() et alia to define ordering of instances.

Ignacio Vazquez-Abrams
Okay, I did and it works.But wouldn't it be a good idea to do this automatically for all Django models?
cool-RR
File a RFE if you feel that passionately about this. http://code.djangoproject.com/
Ignacio Vazquez-Abrams
@cool-RR: Not it is not a good idea. *Default ordering* doesn't necessarily mean that two objects are also comparable that way. E.g. on SO the default ordering of the users is via name, but maybe a user is "larger" than another depending on his reputation. This is really a matter of context and just applying the default order also as natural order is a huge intervention into the business model imoh.
Felix Kling