meta.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ===================
  2. Model ``_meta`` API
  3. ===================
  4. .. module:: django.db.models.options
  5. :synopsis: Model meta-class layer
  6. .. class:: Options
  7. The model ``_meta`` API is at the core of the Django ORM. It enables other
  8. parts of the system such as lookups, queries, forms, and the admin to
  9. understand the capabilities of each model. The API is accessible through
  10. the ``_meta`` attribute of each model class, which is an instance of an
  11. ``django.db.models.options.Options`` object.
  12. Methods and attributes that it provides can be used to:
  13. * Retrieve all field instances of a model
  14. * Retrieve a single field instance of a model by name
  15. * Retrieve all fields that compose the primary key of a model
  16. .. _model-meta-field-api:
  17. Field access API
  18. ================
  19. Retrieving a single field instance of a model by name
  20. -----------------------------------------------------
  21. .. method:: Options.get_field(field_name)
  22. Returns the field instance given a name of a field.
  23. ``field_name`` can be the name of a field on the model, a field
  24. on an abstract or inherited model, or a field defined on another
  25. model that points to the model. In the latter case, the ``field_name``
  26. will be (in order of preference) the :attr:`~.ForeignKey.related_query_name`
  27. set by the user, the :attr:`~.ForeignKey.related_name` set by the user, or
  28. the name automatically generated by Django.
  29. :attr:`Hidden fields <django.db.models.Field.hidden>` cannot be retrieved
  30. by name.
  31. If a field with the given name is not found a
  32. :class:`~django.core.exceptions.FieldDoesNotExist` exception will be
  33. raised.
  34. .. code-block:: pycon
  35. >>> from django.contrib.auth.models import User
  36. # A field on the model
  37. >>> User._meta.get_field("username")
  38. <django.db.models.fields.CharField: username>
  39. # A field from another model that has a relation with the current model
  40. >>> User._meta.get_field("logentry")
  41. <ManyToOneRel: admin.logentry>
  42. # A non existent field
  43. >>> User._meta.get_field("does_not_exist")
  44. Traceback (most recent call last):
  45. ...
  46. FieldDoesNotExist: User has no field named 'does_not_exist'
  47. Retrieving all field instances of a model
  48. -----------------------------------------
  49. .. method:: Options.get_fields(include_parents=True, include_hidden=False)
  50. Returns a tuple of fields associated with a model. ``get_fields()`` accepts
  51. two parameters that can be used to control which fields are returned:
  52. ``include_parents``
  53. ``True`` by default. Recursively includes fields defined on parent
  54. classes. If set to ``False``, ``get_fields()`` will only search for
  55. fields declared directly on the current model. Fields from models that
  56. directly inherit from abstract models or proxy classes are considered
  57. to be local, not on the parent.
  58. ``include_hidden``
  59. ``False`` by default. If set to ``True``, ``get_fields()`` will include
  60. :attr:`hidden fields <django.db.models.Field.hidden>`.
  61. .. code-block:: pycon
  62. >>> from django.contrib.auth.models import User
  63. >>> User._meta.get_fields()
  64. (<ManyToOneRel: admin.logentry>,
  65. <django.db.models.fields.AutoField: id>,
  66. <django.db.models.fields.CharField: password>,
  67. <django.db.models.fields.DateTimeField: last_login>,
  68. <django.db.models.fields.BooleanField: is_superuser>,
  69. <django.db.models.fields.CharField: username>,
  70. <django.db.models.fields.CharField: first_name>,
  71. <django.db.models.fields.CharField: last_name>,
  72. <django.db.models.fields.EmailField: email>,
  73. <django.db.models.fields.BooleanField: is_staff>,
  74. <django.db.models.fields.BooleanField: is_active>,
  75. <django.db.models.fields.DateTimeField: date_joined>,
  76. <django.db.models.fields.related.ManyToManyField: groups>,
  77. <django.db.models.fields.related.ManyToManyField: user_permissions>)
  78. # Also include hidden fields.
  79. >>> User._meta.get_fields(include_hidden=True)
  80. (<ManyToOneRel: auth.user_groups>,
  81. <ManyToOneRel: auth.user_user_permissions>,
  82. <ManyToOneRel: admin.logentry>,
  83. <django.db.models.fields.AutoField: id>,
  84. <django.db.models.fields.CharField: password>,
  85. <django.db.models.fields.DateTimeField: last_login>,
  86. <django.db.models.fields.BooleanField: is_superuser>,
  87. <django.db.models.fields.CharField: username>,
  88. <django.db.models.fields.CharField: first_name>,
  89. <django.db.models.fields.CharField: last_name>,
  90. <django.db.models.fields.EmailField: email>,
  91. <django.db.models.fields.BooleanField: is_staff>,
  92. <django.db.models.fields.BooleanField: is_active>,
  93. <django.db.models.fields.DateTimeField: date_joined>,
  94. <django.db.models.fields.related.ManyToManyField: groups>,
  95. <django.db.models.fields.related.ManyToManyField: user_permissions>)
  96. Retrieving fields composing the primary key of a model
  97. ------------------------------------------------------
  98. .. versionadded:: 5.2
  99. .. attribute:: Options.pk_fields
  100. Returns a list of the fields composing the primary key of a model.
  101. When a :class:`composite primary key <django.db.models.CompositePrimaryKey>`
  102. is defined on a model it will contain all the
  103. :class:`fields <django.db.models.Field>` referenced by it.
  104. .. code-block:: python
  105. from django.db import models
  106. class TenantUser(models.Model):
  107. pk = models.CompositePrimaryKey("tenant_id", "id")
  108. tenant_id = models.IntegerField()
  109. id = models.IntegerField()
  110. .. code-block:: pycon
  111. >>> TenantUser._meta.pk_fields
  112. [
  113. <django.db.models.fields.IntegerField: tenant_id>,
  114. <django.db.models.fields.IntegerField: id>
  115. ]
  116. Otherwise it will contain the single field declared as the
  117. :attr:`primary key <django.db.models.Field.primary_key>` of the model.
  118. .. code-block:: pycon
  119. >>> User._meta.pk_fields
  120. [<django.db.models.fields.AutoField: id>]