Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM ordering vs Model "Meta" ordering - Django 1.11

Tags:

python

django

Small question - what is better way for ordering items:
1)

class Table(models.Model):
    ...
    class Meta:
        ordering = ['user']

2) Table.objects.all().order_by("user")

I think second is better cuz first always will return ordered querysets, and if sometimes we don't need order - it will take additional time? Are there any other reasons to use 1 approach, exсept convenience of "not-writing" .order_by("user") every time?

like image 863
Chiefir Avatar asked Jan 29 '26 00:01

Chiefir


1 Answers

You are right, Meta Ordering does result in additional time if it is not required. The amount of extra overhead can vary depending on the ordering specified.

This is described in the docs here:

https://docs.djangoproject.com/en/1.11/ref/models/options/#ordering

In light of that, it becomes a design choice. If you always need the Model instances to be in the same order, such as an alphabetical list of product names for sale, then you may want Meta Ordering. If you are processing the Model instances in all kinds of different ways which require different ordering/filtering, then you probably wouldn't want to incur the overhead of Meta Ordering.

like image 85
EngineerCamp Avatar answered Jan 30 '26 13:01

EngineerCamp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!