Model Meta Options
In this page:
What Is the Meta Class?
Meta is a small class you nest inside a model to configure things about the model itself, separate from its fields.
Example: What Is the Meta Class?
The Meta class here tells Django to always sort Chapter querysets by the order field unless order_by() overrides it.
from django.db import models
class Chapter(models.Model):
title = models.CharField(max_length=200)
order = models.IntegerField()
class Meta:
ordering = ['order']
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Setting a Default Ordering
The ordering option is a list of field names that becomes the default sort order for every queryset on that model.
Note: Prefix a field with - to sort that field descending by default.
Example: Setting a Default Ordering
Now Tutorial.objects.all() automatically returns rows sorted by order, without needing order_by(order) every time.
class Tutorial(models.Model):
title = models.CharField(max_length=200)
order = models.IntegerField()
class Meta:
ordering = ['order']
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Enforcing Uniqueness with unique_together
unique_together makes sure a combination of fields never repeats across rows, even if each field alone could repeat.
Example: Enforcing Uniqueness with unique_together
This allows the same slug to be reused across different languages, but never twice within the same language.
class Tutorial(models.Model):
language_id = models.IntegerField()
slug = models.SlugField()
class Meta:
unique_together = ('language_id', 'slug')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Forgetting that Meta must be an inner class of the model, not a separate top-level class.
- Setting ordering in Meta and expecting it to override an explicit order_by() call — order_by() always wins when both are present.
- Misspelling option names like orderin instead of ordering, which Django silently ignores instead of raising an error for unknown Meta attributes in some Django versions.
- class Meta is a nested class inside a model used to configure model-wide behavior.
- Common options include ordering, verbose_name, and unique_together.
- Meta options apply automatically without changing how you create or query objects.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: