Joff
Joff

Reputation: 12187

In what order does Django (with postgres) return db queries?

I had some stuff on my site that was displaying in a different order depending on what was last edited in the db.

I confirmed that in the shell, the order of what was returned for

>>>foo.objects.all()

was dependent on what was last edited. I know how I can make the objects display in the order I want, but I could not come up with an explanation for why it was pulling them by the last edit. What field in the database is it going by? Is the database simply ordered differently by what was edited?

Upvotes: 0

Views: 438

Answers (1)

DanielB
DanielB

Reputation: 2958

If you want to set a default ordering, use the ordering meta field

class Foo(models.Model):
  # ...
  class Meta:
    ordering = ['some_field', '-another_field']

If you do not specify this, the ordering is dependent on the underlying storage system. Have a look at this answer and the comment by solarissmoke on your question.

You can later override this using the order_by method of a queryset if needed.

Upvotes: 2

Related Questions