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