Model _meta API

class Options[source]

The model _meta API is at the core of the Django ORM. It enables other parts of the system such as lookups, queries, forms, and the admin to understand the capabilities of each model. The API is accessible through the _meta attribute of each model class, which is an instance of an django.db.models.options.Options object.

Methods that it provides can be used to:

  • Retrieve all field instances of a model
  • Retrieve a single field instance of a model by name

Field access API

Retrieving a single field instance of a model by name

Options.get_field(field_name)[source]

Returns the field instance given a name of a field.

field_name can be the name of a field on the model, a field on an abstract or inherited model, or a field defined on another model that points to the model. In the latter case, the field_name will be (in order of preference) the related_query_name set by the user, the related_name set by the user, or the name automatically generated by Django.

Hidden fields cannot be retrieved by name.

If a field with the given name is not found a FieldDoesNotExist exception will be raised.

  1. >>> from django.contrib.auth.models import User
  2. # A field on the model
  3. >>> User._meta.get_field("username")
  4. <django.db.models.fields.CharField: username>
  5. # A field from another model that has a relation with the current model
  6. >>> User._meta.get_field("logentry")
  7. <ManyToOneRel: admin.logentry>
  8. # A non existent field
  9. >>> User._meta.get_field("does_not_exist")
  10. Traceback (most recent call last):
  11. ...
  12. FieldDoesNotExist: User has no field named 'does_not_exist'

Retrieving all field instances of a model

Options.get_fields(include_parents=True, include_hidden=False)[source]

Returns a tuple of fields associated with a model. get_fields() accepts two parameters that can be used to control which fields are returned:

  • include_parents

    True by default. Recursively includes fields defined on parent classes. If set to False, get_fields() will only search for fields declared directly on the current model. Fields from models that directly inherit from abstract models or proxy classes are considered to be local, not on the parent.

    include_hidden

    False by default. If set to True, get_fields() will include hidden fields.

  1. >>> from django.contrib.auth.models import User
  2. >>> User._meta.get_fields()
  3. (<ManyToOneRel: admin.logentry>,
  4. <django.db.models.fields.AutoField: id>,
  5. <django.db.models.fields.CharField: password>,
  6. <django.db.models.fields.DateTimeField: last_login>,
  7. <django.db.models.fields.BooleanField: is_superuser>,
  8. <django.db.models.fields.CharField: username>,
  9. <django.db.models.fields.CharField: first_name>,
  10. <django.db.models.fields.CharField: last_name>,
  11. <django.db.models.fields.EmailField: email>,
  12. <django.db.models.fields.BooleanField: is_staff>,
  13. <django.db.models.fields.BooleanField: is_active>,
  14. <django.db.models.fields.DateTimeField: date_joined>,
  15. <django.db.models.fields.related.ManyToManyField: groups>,
  16. <django.db.models.fields.related.ManyToManyField: user_permissions>)
  17. # Also include hidden fields.
  18. >>> User._meta.get_fields(include_hidden=True)
  19. (<ManyToOneRel: auth.user_groups>,
  20. <ManyToOneRel: auth.user_user_permissions>,
  21. <ManyToOneRel: admin.logentry>,
  22. <django.db.models.fields.AutoField: id>,
  23. <django.db.models.fields.CharField: password>,
  24. <django.db.models.fields.DateTimeField: last_login>,
  25. <django.db.models.fields.BooleanField: is_superuser>,
  26. <django.db.models.fields.CharField: username>,
  27. <django.db.models.fields.CharField: first_name>,
  28. <django.db.models.fields.CharField: last_name>,
  29. <django.db.models.fields.EmailField: email>,
  30. <django.db.models.fields.BooleanField: is_staff>,
  31. <django.db.models.fields.BooleanField: is_active>,
  32. <django.db.models.fields.DateTimeField: date_joined>,
  33. <django.db.models.fields.related.ManyToManyField: groups>,
  34. <django.db.models.fields.related.ManyToManyField: user_permissions>)