Python/Django debugging: print model's containing data


Maybe easy question but I don't know how to summarize it that I would find my answer.

Is it possible to print out all available fields of model?

For example in iPython I can import model and just write model name and tab will show all available fields the models have.

Is it possible to do this in code without using some sort of shell?

I would like to use some sort of command (e.a. print_fields(self)) and get what's inside the model.

12/2/2015 9:51:54 AM

Accepted Answer

To check fields on a model I usually use ?:

>>> Person?
Type:       ModelBase
Base Class: <class 'django.db.models.base.ModelBase'>
String Form:    <class ''>
Namespace:  Interactive
File:       /home/zk/ve/django/foo/bar/
    Person(id, first_name, last_name)

You can also use help(). If you have an instance of the model you can look at __dict__:

>>> [x for x in Person().__dict__.keys() if not x.startswith('_')]
<<< ['first_name', 'last_name', 'id']
5/31/2011 3:57:51 PM

If you have the model instance(s) you can simply call:


For just the list of fields using the model class itself see Django: Get list of model fields?

or directly the documentation - for Django 1.10 there is a dedicated section on how to work with fields:

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow