_meta
API¶模型 _meta
的 API 是 Django ORM 的核心。它使系统的其他部分,如查找、查询、表单和管理能够理解每个模型的功能。API 可以通过每个模型类的 _meta
属性来访问,它是 django.db.models.options.Options
对象的一个实例。
Methods and attributes that it provides can be used to:
检索一个模型的所有字段实例
按名称检索模型的单个字段实例
Retrieve all fields that compose the primary key of a model
返回给定字段名的字段实例。
field_name
可以是模型上的字段名,也可以是抽象或继承的模型上的字段名,或者是指向模型的另一个模型上定义的字段名。在后一种情况下,field_name
将是(按优先级排序)用户设置的 related_query_name
,用户设置的 related_name
,或者 Django 自动生成的名称。
隐藏字段
不能按名称检索。
如果没有找到指定名称的字段,将引发一个 FieldDoesNotExist
异常。
>>> from django.contrib.auth.models import User
# A field on the model
>>> User._meta.get_field("username")
<django.db.models.fields.CharField: username>
# A field from another model that has a relation with the current model
>>> User._meta.get_field("logentry")
<ManyToOneRel: admin.logentry>
# A non existent field
>>> User._meta.get_field("does_not_exist")
Traceback (most recent call last):
...
FieldDoesNotExist: User has no field named 'does_not_exist'
返回与模型相关联的字段的元组。get_fields()
接受两个参数,可以用来控制返回哪些字段:
include_parents
默认为 True
。递归地包括父类上定义的字段。如果设置为 False
,get_fields()
将只搜索直接在当前模型上声明的字段。直接继承自抽象模型或代理类的模型的字段被认为是本地的,而不是在父类上。
include_hidden
默认情况下为 False
。如果设置为 True
,get_fields()
将包括 隐藏字段
。
>>> from django.contrib.auth.models import User
>>> User._meta.get_fields()
(<ManyToOneRel: admin.logentry>,
<django.db.models.fields.AutoField: id>,
<django.db.models.fields.CharField: password>,
<django.db.models.fields.DateTimeField: last_login>,
<django.db.models.fields.BooleanField: is_superuser>,
<django.db.models.fields.CharField: username>,
<django.db.models.fields.CharField: first_name>,
<django.db.models.fields.CharField: last_name>,
<django.db.models.fields.EmailField: email>,
<django.db.models.fields.BooleanField: is_staff>,
<django.db.models.fields.BooleanField: is_active>,
<django.db.models.fields.DateTimeField: date_joined>,
<django.db.models.fields.related.ManyToManyField: groups>,
<django.db.models.fields.related.ManyToManyField: user_permissions>)
# Also include hidden fields.
>>> User._meta.get_fields(include_hidden=True)
(<ManyToOneRel: auth.user_groups>,
<ManyToOneRel: auth.user_user_permissions>,
<ManyToOneRel: admin.logentry>,
<django.db.models.fields.AutoField: id>,
<django.db.models.fields.CharField: password>,
<django.db.models.fields.DateTimeField: last_login>,
<django.db.models.fields.BooleanField: is_superuser>,
<django.db.models.fields.CharField: username>,
<django.db.models.fields.CharField: first_name>,
<django.db.models.fields.CharField: last_name>,
<django.db.models.fields.EmailField: email>,
<django.db.models.fields.BooleanField: is_staff>,
<django.db.models.fields.BooleanField: is_active>,
<django.db.models.fields.DateTimeField: date_joined>,
<django.db.models.fields.related.ManyToManyField: groups>,
<django.db.models.fields.related.ManyToManyField: user_permissions>)
Returns a list of the fields composing the primary key of a model.
When a composite primary key
is defined on a model it will contain all the
fields
referenced by it.
from django.db import models
class TenantUser(models.Model):
pk = models.CompositePrimaryKey("tenant_id", "id")
tenant_id = models.IntegerField()
id = models.IntegerField()
>>> TenantUser._meta.pk_fields
[
<django.db.models.fields.IntegerField: tenant_id>,
<django.db.models.fields.IntegerField: id>
]
Otherwise it will contain the single field declared as the
primary key
of the model.
>>> User._meta.pk_fields
[<django.db.models.fields.AutoField: id>]
4月 27, 2025