Python/Django debugging: print model's containing data Python/Django debugging: print model's containing data django django

Python/Django debugging: print model's containing data


To check fields on a model I usually use ?:

>>> Person?Type:       ModelBaseBase Class: <class 'django.db.models.base.ModelBase'>String Form:    <class 'foo.bar.models.Person'>Namespace:  InteractiveFile:       /home/zk/ve/django/foo/bar/models.pyDocstring:    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']


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

model_queryset.all().values()

https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values

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: https://docs.djangoproject.com/en/1.10/ref/models/meta/#retrieving-all-field-instances-of-a-model


I think you just want to use __dict__ on an instance of a model. (It won't give methods like tab completion in ipython though). Also using __doc__ is quite helpful usually.

Also look into inspect http://docs.python.org/library/inspect.html