Skip to main content

Model Meta Options

The Model Meta class contains metadata about the model that is not field-specific.

Usage

class MyModel(models.Model):
    # Fields
    name = models.CharField(max_length=100)
    
    class Meta:
        db_table = 'my_custom_table'
        ordering = ['-created']
        verbose_name = 'My Model'
Reference: django/db/models/options.py:90

Available Meta Options

abstract

If True, this model will be an abstract base class.
class Meta:
    abstract = True
abstract
bool
default:"False"
When True, the model is treated as an abstract base class and no database table is created.
Reference: django/db/models/options.py:146

app_label

Defines which app the model belongs to. Rarely needed as Django can usually determine this automatically.
class Meta:
    app_label = 'myapp'
app_label
str
The name of the app that contains this model.
Reference: django/db/models/options.py:137

base_manager_name

The name of the manager to use for _base_manager.
class Meta:
    base_manager_name = 'objects'
base_manager_name
str
The attribute name of the manager to use as the base manager.
Reference: django/db/models/options.py:121

db_table

The name of the database table to use for the model.
class Meta:
    db_table = 'my_custom_table_name'
db_table
str
The database table name. If not provided, Django constructs one from app_label and model name.
Reference: django/db/models/options.py:126

db_table_comment

A comment on the database table. Requires database support for comments.
class Meta:
    db_table_comment = 'This table stores customer information'
db_table_comment
str
A comment to add to the database table.
Reference: django/db/models/options.py:127

db_tablespace

The database tablespace to use for this model’s table.
class Meta:
    db_tablespace = 'my_tablespace'
db_tablespace
str
The name of the database tablespace. Falls back to DEFAULT_TABLESPACE setting.
Reference: django/db/models/options.py:140

default_manager_name

The name of the manager to use as the default manager.
class Meta:
    default_manager_name = 'objects'
default_manager_name
str
The attribute name of the manager to use as the default manager.
Reference: django/db/models/options.py:122

default_permissions

Defaults to ('add', 'change', 'delete', 'view'). Customize to change the permissions created automatically.
class Meta:
    default_permissions = ('add', 'change', 'delete')
    # or
    default_permissions = ()  # No default permissions
default_permissions
tuple
default:"('add', 'change', 'delete', 'view')"
A tuple/list of permission names to create automatically.
Reference: django/db/models/options.py:134 The name that will be used by default for the relation from a related object back to this one.
class Meta:
    default_related_name = '%(app_label)s_%(model_name)s_related'
Default related_name for reverse relations. Supports %(app_label)s, %(model_name)s, and %(class)s substitution.
Reference: django/db/models/options.py:170

get_latest_by

The name of a field or list of field names to use for the latest() and earliest() methods.
class Meta:
    get_latest_by = 'created'
    # or
    get_latest_by = ['created', 'modified']
get_latest_by
str | list[str]
Field name(s) to order by for latest() and earliest() methods.
Reference: django/db/models/options.py:138

managed

Defaults to True. If False, no database table creation, modification, or deletion will be performed for this model.
class Meta:
    managed = False  # Don't create/modify table
managed
bool
default:"True"
If False, Django will not create or modify database tables for this model.
Reference: django/db/models/options.py:147

order_with_respect_to

Makes this object orderable with respect to the given field.
class Answer(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    
    class Meta:
        order_with_respect_to = 'question'
order_with_respect_to
str
Field name that defines the ordering relationship.
Reference: django/db/models/options.py:139

ordering

The default ordering for the object.
class Meta:
    ordering = ['-created', 'name']
    # or
    ordering = ['?']  # Random ordering
ordering
list[str]
List of field names to order by. Prefix with ’-’ for descending order. Use ’?’ for random ordering.
Reference: django/db/models/options.py:128

permissions

Extra permissions to enter into the permissions table when creating this object.
class Meta:
    permissions = [
        ('can_publish', 'Can publish articles'),
        ('can_archive', 'Can archive articles'),
    ]
permissions
list[tuple]
List of 2-tuples in the format (permission_code, human_readable_permission_name).
Reference: django/db/models/options.py:135

proxy

If True, a model that subclasses another model will be treated as a proxy model.
class MyPerson(Person):
    class Meta:
        proxy = True
    
    def do_something(self):
        # Custom method
        pass
proxy
bool
default:"False"
If True, creates a proxy model that doesn’t create a new database table.
Reference: django/db/models/options.py:148

required_db_features

List of database features that the current connection should have so that the model is considered during migration.
class Meta:
    required_db_features = ['gis_enabled']
required_db_features
list[str]
List of database feature names required for this model.
Reference: django/db/models/options.py:141

required_db_vendor

The name of a supported database vendor that this model is specific to.
class Meta:
    required_db_vendor = 'postgresql'
required_db_vendor
str
Database vendor name (e.g., ‘postgresql’, ‘mysql’, ‘sqlite’, ‘oracle’).
Reference: django/db/models/options.py:142

select_on_save

Determines if Django will use pre-1.6 select behavior when saving objects.
class Meta:
    select_on_save = True
select_on_save
bool
default:"False"
If True, saves will use SELECT to determine whether to INSERT or UPDATE.
Reference: django/db/models/options.py:133

indexes

A list of indexes that you want to define on the model.
from django.db import models

class Meta:
    indexes = [
        models.Index(fields=['last_name', 'first_name']),
        models.Index(fields=['created'], name='created_idx'),
    ]
indexes
list[Index]
List of Index objects to create on the model’s table.
Reference: django/db/models/options.py:130

constraints

A list of constraints that you want to define on the model.
from django.db import models

class Meta:
    constraints = [
        models.CheckConstraint(
            check=models.Q(age__gte=18),
            name='age_gte_18',
        ),
        models.UniqueConstraint(
            fields=['email'],
            name='unique_email',
        ),
    ]
constraints
list[Constraint]
List of Constraint objects to apply to the model’s table.
Reference: django/db/models/options.py:131

unique_together

Sets of field names that, taken together, must be unique.
class Meta:
    unique_together = [['driver', 'restaurant']]
    # or
    unique_together = ['driver', 'restaurant']  # Single constraint
unique_together
list[list[str]]
A list of lists of field names that must be unique together. Deprecated in favor of UniqueConstraint.
Reference: django/db/models/options.py:132

verbose_name

A human-readable name for the object, singular.
class Meta:
    verbose_name = 'person'
verbose_name
str
Human-readable name for the model (singular). If not provided, Django creates one from the class name.
Reference: django/db/models/options.py:124

verbose_name_plural

The plural name for the object.
class Meta:
    verbose_name_plural = 'people'
verbose_name_plural
str
Human-readable plural name. Defaults to verbose_name + ‘s’.
Reference: django/db/models/options.py:125

Read-Only Meta Attributes

These attributes are available on Model._meta but cannot be set in the Meta class:

label

Representation of the model in the format app_label.ModelName.
MyModel._meta.label  # 'myapp.MyModel'
Reference: django/db/models/options.py:173

label_lower

Lowercase version of label.
MyModel._meta.label_lower  # 'myapp.mymodel'
Reference: django/db/models/options.py:177

object_name

The name of the model class.
MyModel._meta.object_name  # 'MyModel'
Reference: django/db/models/options.py:136

model_name

Lowercase version of the model name.
MyModel._meta.model_name  # 'mymodel'
Reference: django/db/models/options.py:123

Methods

get_field()

Returns a field instance given a field name.
field = MyModel._meta.get_field('name')
field_name
str
required
The name of the field to retrieve.
return
Field
The Field instance.
Reference: django/db/models/options.py:670

get_fields()

Returns a tuple of fields associated with the model.
fields = MyModel._meta.get_fields()
fields = MyModel._meta.get_fields(include_hidden=True)
include_parents
bool
default:"True"
Include fields from parent classes.
include_hidden
bool
default:"False"
Include fields with related_name starting with ’+’.
return
tuple[Field]
Tuple of Field instances.
Reference: django/db/models/options.py:865

Build docs developers (and LLMs) love