15

Is there any way to to list out the fields present in a table in django models

 class Profile(models.Model):
    user = models.ForeignKey(User, unique=True)
    name = models.ForeignKey(School)
    emp = models.ForeignKey(User, unique=True)

How to list out the filed names from the table Profile,(just like desc Profile; in mysql )

thanks.

Hulk
  • 32,860
  • 62
  • 144
  • 215

1 Answers1

19

Profile._meta.fields will get you a list of fields. The name property of the field object contains the name of the field. Profile._meta.get_fields_with_model() will return a list of 2-tuples of (field, model), with model being None if the field is in Profile.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358