Get all fields of a model in Django Shell

6.2k views Asked by At

Is it possible to output names of all fields of a specific model in Django Shell? For example, how to get fields from User model:

>>> from django.contrib.auth.models import User

I know I can get it by accessing models.py of the appropriate app but it would be more convenient for me to get information on models of all the apps at the same place.

2

There are 2 answers

3
Lewis On BEST ANSWER

To return a list of field objects of the User model. Use:

field_names = User._meta.get_fields()

To return a list of field names for User model, Use:

field_names = [f.name for f in User._meta.get_fields()]
2
Piwero On

Also, you can see only the model (without foreign keys fields) by User._meta.fields. is there any way to only see the required fields there?