Django Get All Fields of Model Example

Published On: 26/07/2022 | Category: Django Python

Hi Dev,

This article goes in detailed on django get all fields of model example. We will use django get all fields of model. we will help you to give example of django model get all field names. we will help you to give example of django get all fields.

So, Django provide a _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 mode

Here Now let’s see example of django Retrieving all field instances of a model. Let’s start:

Step 6 : Adding values to the Model

So, in this step let’s start our shell now. and fetching some records in user table.

python manage.py shell

Retrieving all field instances of a model:

Syntax:
Options.get_fields(include_parents=True, include_hidden=False)
  • 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 “+”.
Example : 1

In [1]: from django.contrib.auth.models import User

In [2]: User._meta.get_fields()
Output
Out[2]: 
(<OneToOneRel: admin_black.adminblacksetting>,
 <ManyToOneRel: admin.logentry>,
 <django.db.models.fields.AutoField: id>,
 <django.db.models.fields.CharField: password>,
 <django.db.models.fields.DateTimeField: last_login>,
 <django.db.models.fields.BooleanField: is_superuser>,
 <django.db.models.fields.CharField: username>,
 <django.db.models.fields.CharField: first_name>,
 <django.db.models.fields.CharField: last_name>,
 <django.db.models.fields.EmailField: email>,
 <django.db.models.fields.BooleanField: is_staff>,
 <django.db.models.fields.BooleanField: is_active>,
 <django.db.models.fields.DateTimeField: date_joined>,
 <django.db.models.fields.related.ManyToManyField: groups>,
 <django.db.models.fields.related.ManyToManyField: user_permissions>)
Example : 2

Also include hidden fields set include_hidden is True.


In [1]: from django.contrib.auth.models import User

In [2]: User._meta.get_fields(include_hidden=True)
Output
Out[2]: 
(<OneToOneRel: admin_black.adminblacksetting>,
 <ManyToOneRel: admin.logentry>,
 <ManyToOneRel: auth.user_groups>,
 <ManyToOneRel: auth.user_user_permissions>,
 <django.db.models.fields.AutoField: id>,
 <django.db.models.fields.CharField: password>,
 <django.db.models.fields.DateTimeField: last_login>,
 <django.db.models.fields.BooleanField: is_superuser>,
 <django.db.models.fields.CharField: username>,
 <django.db.models.fields.CharField: first_name>,
 <django.db.models.fields.CharField: last_name>,
 <django.db.models.fields.EmailField: email>,
 <django.db.models.fields.BooleanField: is_staff>,
 <django.db.models.fields.BooleanField: is_active>,
 <django.db.models.fields.DateTimeField: date_joined>,
 <django.db.models.fields.related.ManyToManyField: groups>,
 <django.db.models.fields.related.ManyToManyField: user_permissions>)

I Hope It will help you....