meta.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 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. .. _model-meta-field-api:
  16. Field access API
  17. ================
  18. Retrieving a single field instance of a model by name
  19. -----------------------------------------------------
  20. .. method:: Options.get_field(field_name)
  21. Returns the field instance given a name of a field.
  22. ``field_name`` can be the name of a field on the model, a field
  23. on an abstract or inherited model, or a field defined on another
  24. model that points to the model. In the latter case, the ``field_name``
  25. will be the ``related_name`` defined by the user or the name automatically
  26. generated by Django itself.
  27. :attr:`Hidden fields <django.db.models.Field.hidden>` cannot be retrieved
  28. by name.
  29. If a field with the given name is not found a
  30. :class:`~django.core.exceptions.FieldDoesNotExist` exception will be
  31. raised.
  32. .. code-block:: python
  33. >>> from django.contrib.auth.models import User
  34. # A field on the model
  35. >>> User._meta.get_field('username')
  36. <django.db.models.fields.CharField: username>
  37. # A field from another model that has a relation with the current model
  38. >>> User._meta.get_field('logentry')
  39. <ManyToOneRel: admin.logentry>
  40. # A non existent field
  41. >>> User._meta.get_field('does_not_exist')
  42. Traceback (most recent call last):
  43. ...
  44. FieldDoesNotExist: User has no field named 'does_not_exist'
  45. Retrieving all field instances of a model
  46. -----------------------------------------
  47. .. method:: Options.get_fields(include_parents=True, include_hidden=False)
  48. Returns a tuple of fields associated with a model. ``get_fields()`` accepts
  49. two parameters that can be used to control which fields are returned:
  50. ``include_parents``
  51. ``True`` by default. Recursively includes fields defined on parent
  52. classes. If set to ``False``, ``get_fields()`` will only search for
  53. fields declared directly on the current model. Fields from models that
  54. directly inherit from abstract models or proxy classes are considered
  55. to be local, not on the parent.
  56. ``include_hidden``
  57. ``False`` by default. If set to ``True``, ``get_fields()`` will include
  58. fields that are used to back other field's functionality. This will
  59. also include any fields that have a ``related_name`` (such
  60. as :class:`~django.db.models.ManyToManyField`, or
  61. :class:`~django.db.models.ForeignKey`) that start with a "+".
  62. .. code-block:: python
  63. >>> from django.contrib.auth.models import User
  64. >>> User._meta.get_fields()
  65. (<ManyToOneRel: admin.logentry>,
  66. <django.db.models.fields.AutoField: id>,
  67. <django.db.models.fields.CharField: password>,
  68. <django.db.models.fields.DateTimeField: last_login>,
  69. <django.db.models.fields.BooleanField: is_superuser>,
  70. <django.db.models.fields.CharField: username>,
  71. <django.db.models.fields.CharField: first_name>,
  72. <django.db.models.fields.CharField: last_name>,
  73. <django.db.models.fields.EmailField: email>,
  74. <django.db.models.fields.BooleanField: is_staff>,
  75. <django.db.models.fields.BooleanField: is_active>,
  76. <django.db.models.fields.DateTimeField: date_joined>,
  77. <django.db.models.fields.related.ManyToManyField: groups>,
  78. <django.db.models.fields.related.ManyToManyField: user_permissions>)
  79. # Also include hidden fields.
  80. >>> User._meta.get_fields(include_hidden=True)
  81. (<ManyToOneRel: auth.user_groups>,
  82. <ManyToOneRel: auth.user_user_permissions>,
  83. <ManyToOneRel: admin.logentry>,
  84. <django.db.models.fields.AutoField: id>,
  85. <django.db.models.fields.CharField: password>,
  86. <django.db.models.fields.DateTimeField: last_login>,
  87. <django.db.models.fields.BooleanField: is_superuser>,
  88. <django.db.models.fields.CharField: username>,
  89. <django.db.models.fields.CharField: first_name>,
  90. <django.db.models.fields.CharField: last_name>,
  91. <django.db.models.fields.EmailField: email>,
  92. <django.db.models.fields.BooleanField: is_staff>,
  93. <django.db.models.fields.BooleanField: is_active>,
  94. <django.db.models.fields.DateTimeField: date_joined>,
  95. <django.db.models.fields.related.ManyToManyField: groups>,
  96. <django.db.models.fields.related.ManyToManyField: user_permissions>)