Provide Metadata for the Model in Django

In Django, the Meta class is an inner class that provides metadata to the model. This metadata includes options that affect the behavior of the model, such as its database table name, ordering, and permissions. Here is an overview of some commonly used Meta options:

Common Meta Options

  1. db_table:
    • Specifies the name of the database table to use for the model.
    class MyModel(models.Model):
        name = models.CharField(max_length=100)
    
        class Meta:
            db_table = 'my_custom_table'
    
  2. ordering:
    • Specifies the default ordering for the model records when they are retrieved from the database.
    class MyModel(models.Model):
        name = models.CharField(max_length=100)
        created_at = models.DateTimeField(auto_now_add=True)
    
        class Meta:
            ordering = ['created_at']
    
  3. verbose_name:
    • A human-readable name for the model.
    class MyModel(models.Model):
        name = models.CharField(max_length=100)
    
        class Meta:
            verbose_name = 'My Custom Model'
    
  4. verbose_name_plural:
    • The plural name for the model.
    class MyModel(models.Model):
        name = models.CharField(max_length=100)
    
        class Meta:
            verbose_name_plural = 'My Custom Models'
    
  5. unique_together:
    • Ensures that a set of fields must be unique together.
    class MyModel(models.Model):
        first_name = models.CharField(max_length=30)
        last_name = models.CharField(max_length=30)
    
        class Meta:
            unique_together = ('first_name', 'last_name')
    
  6. index_together:
    • Creates a composite index for a set of fields.
    class MyModel(models.Model):
        first_name = models.CharField(max_length=30)
        last_name = models.CharField(max_length=30)
    
        class Meta:
            index_together = ['first_name', 'last_name']
    
  7. permissions:
    • Custom permissions for the model.
    class MyModel(models.Model):
        name = models.CharField(max_length=100)
    
        class Meta:
            permissions = [
                ('can_view', 'Can view the model'),
            ]
    
  8. abstract:
    • If set to True, this model will be an abstract base class.
    class MyBaseModel(models.Model):
        created_at = models.DateTimeField(auto_now_add=True)
    
        class Meta:
            abstract = True
    
    class MyModel(MyBaseModel):
        name = models.CharField(max_length=100)
    

Example of a Django Model with Meta Options

from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'product'
        ordering = ['-created_at']
        verbose_name = 'Product'
        verbose_name_plural = 'Products'
        unique_together = ('name', 'price')
        index_together = ['name', 'created_at']
        permissions = [
            ('can_view_product', 'Can view product'),
        ]

In this example, the Product model has several Meta options specified, including custom table name, default ordering, unique constraints, index constraints, and custom permissions.

References
https://docs.djangoproject.com/en/5.0/ref/models/options/