Skip to content Skip to sidebar Skip to footer

Orm Ordering Vs Model "meta" Ordering - Django 1.11

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')

Solution 1:

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.

Solution 2:

When you are using the model in a class-based view it is sometimes important to use the first option because it might be a model passed as a ForeignKey. In this case when referencing it within your template you cannot set the order if you have not added the ordering in Meta.

Post a Comment for "Orm Ordering Vs Model "meta" Ordering - Django 1.11"