Every Field
instance contains several attributes that allow introspecting its behavior. Use these attributes instead of isinstance
checks when you need to write code that depends on a field's functionality. These attributes can be used together with the Model._meta
API to narrow down a search for specific field types. Custom model fields should implement these flags.
Boolean flag that indicates if the field was automatically created, such as the OneToOneField
used by model inheritance.
Boolean flag that indicates if a field is used to back another non-hidden field's functionality (for example, the content_type
and object_id
fields that make up a GenericForeignKey
). The hidden
flag is used to distinguish what constitutes the public subset of fields on the model from all the fields on the model.
Boolean flag that indicates if a field contains references to one or more other models for its functionality (for example, ForeignKey
, ManyToManyField
, OneToOneField
, and others).
These attributes are used to query for the cardinality and other details of a relation. These attribute are present on all fields; however, they will only have meaningful values if the field is a relation type (Field.is_relation=True
).
Boolean flag that is True
if the field has a many-to-many relation; False
otherwise. The only field included with Django where this is True
is ManyToManyField
.
Boolean flag that is True
if the field has a many-to-one relation, such as a ForeignKey
; False
otherwise.
Boolean flag that is True
if the field has a one-to-many relation, such as a GenericRelation
or the reverse of a ForeignKey
; False
otherwise.
Boolean flag that is True
if the field has a one-to-one relation, such as a OneToOneField
; False
otherwise.