5

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.

Sayyor Y
  • 1,130
  • 2
  • 14
  • 27

2 Answers2

8

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()]
Sayyor Y
  • 1,130
  • 2
  • 14
  • 27
Lewis
  • 2,718
  • 1
  • 11
  • 28
1

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?

Piwero
  • 21
  • 4
  • 1
    this can be a new question try asking it as a new one. – Mojtaba Jun 12 '22 at 08:44
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31988634) – Waldi Jun 15 '22 at 12:08