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.

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 the related_name defined by the user or the name automatically
generated by Django itself.

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

>>> fromdjango.contrib.auth.modelsimportUser# A field on the model>>> User._meta.get_field('username')<django.db.models.fields.CharField: username># A field from another model that has a relation with the current model>>> User._meta.get_field('logentry')<ManyToOneRel: admin.logentry># A non existent field>>> User._meta.get_field('does_not_exist')Traceback (most recent call last):...FieldDoesNotExist: User has no field named 'does_not_exist'

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
fields that are used to back other field’s functionality. This will
also include any fields that have a related_name (such
as ManyToManyField, or
ForeignKey) that start with a “+”.

invoking Options.get_fields() to retrieve a list of all
fields, and then filtering this list using the field attributes that describe (or retrieve, in the case of
_with_model variants) the properties of the desired fields.

Although it’s possible to make strictly equivalent replacements of the old
methods, that might not be the best approach. Taking the time to refactor any
field loops to make better use of the new API - and possibly include fields
that were previously excluded - will almost certainly result in better code.

Assuming you have a model named MyModel, the following substitutions
can be made to convert your code to the new API:

MyModel._meta.get_field(name) becomes:

f=MyModel._meta.get_field(name)

then check if:

f.auto_created==False, because the new get_field()
API will find “reverse” relations, and:

f.is_relationandf.related_modelisNone, because the new
get_field() API will find
GenericForeignKey relations.

MyModel._meta.get_field_by_name(name) returns a tuple of these four
values with the following replacements:

The auto_created check excludes
all “forward” and “reverse” relations that are created by Django, but
this also includes AutoField and OneToOneField on proxy models.
We avoid filtering out these attributes using the
concrete attribute.

fromitertoolsimportchainlist(set(chain.from_iterable((field.name,field.attname)ifhasattr(field,'attname')else(field.name,)forfieldinMyModel._meta.get_fields()# For complete backwards compatibility, you may want to exclude# GenericForeignKey from the results.ifnot(field.many_to_oneandfield.related_modelisNone))))

This provides a 100% backwards compatible replacement, ensuring that both
field names and attribute names ForeignKeys are included, but fields
associated with GenericForeignKeys are not. A simpler version would be:

[f.nameforfinMyModel._meta.get_fields()]

While this isn’t 100% backwards compatible, it may be sufficient in many
situations.