Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Class Meta | Advanced
Django ORM Ninja: Advanced Techniques for Developers

book
Class Meta

In Django, the Meta class is a special class within a model that provides metadata about the model. It's used to configure aspects like database table names, ordering, verbose names, and much more. Understanding Meta options is crucial for effectively customizing and optimizing your Django models.

Common Meta Options

  • db_table: specifies the database table name;

  • ordering: determines the default ordering of records;

  • verbose_name: define human-readable singular name for the model;

  • verbose_name_plural: define human-readable plural name for the model;

  • unique_together: sets a tuple of fields that must be unique when considered together;

  • index_together: similar to unique_together, but for indexes;

  • permissions: custom permissions for the model;

  • get_latest_by: Field name to use in the latest() method.

Practical Examples

python
# Genre model with custom table name and ordering
class Genre(models.Model):
format = models.CharField(max_length=100)

class Meta:
db_table = "genre_table"
ordering = ["format"]
verbose_name_plural = "Genres"

# Author model with verbose name and custom permissions
class Author(models.Model):
first_name = models.CharField(max_length=100)

class Meta:
verbose_name = "Author"
verbose_name_plural = "Authors"
permissions = (
("can_edit_author", "Can edit author details"),
("can_delete_author", "Can delete author"),
)

# Book model with unique together and get latest by
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.ManyToManyField(Author, on_delete=models.CASCADE)
genre = models.ForeignKey(Genre, on_delete=models.CASCADE)
publication_date = models.DateField()

class Meta:
unique_together = ("title", "author")
get_latest_by = "publication_date"

In the Book model, unique_together ensures that no two books have the same title and author. get_latest_by makes it easy to retrieve the most recently published book.

Note

To indicate descending order, use an optional - prefix: ordering=["-publication_date"].

1. What is the purpose of the Class Meta in Django models?

2. What does the 'unique_together' Meta option do in a Django model?

3. What does the 'db_table' Meta option do in a Django model?

4. What is the purpose of 'verbose_name' and 'verbose_name_plural' in Django's Meta class?

question mark

What is the purpose of the Class Meta in Django models?

Select the correct answer

question mark

What does the 'unique_together' Meta option do in a Django model?

Select the correct answer

question mark

What does the 'db_table' Meta option do in a Django model?

Select the correct answer

question mark

What is the purpose of 'verbose_name' and 'verbose_name_plural' in Django's Meta class?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 6. Kapitel 1

Fragen Sie AI

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

We use cookies to make your experience better!
some-alt