Ram Rachum
Ram Rachum

Reputation: 88528

Ordering of Django models

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?

Upvotes: 0

Views: 968

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

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.

Upvotes: 2

Related Questions