← Back to Django Course | Chapter 6: Advanced ORM Queries | Lesson 8 of 9

Model Meta Options

Meta options are like a rulebook attached to a model, telling Django extra details about it, like what order to list rows in or what to call it in the admin.

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.

markup
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.

markup
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.

markup
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. #}
Common Mistakes
  1. Forgetting that Meta must be an inner class of the model, not a separate top-level class.
  2. Setting ordering in Meta and expecting it to override an explicit order_by() call — order_by() always wins when both are present.
  3. 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.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.