24๐
To check fields on a model I usually use ?
:
>>> Person?
Type: ModelBase
Base Class: <class 'django.db.models.base.ModelBase'>
String Form: <class 'foo.bar.models.Person'>
Namespace: Interactive
File: /home/zk/ve/django/foo/bar/models.py
Docstring:
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']
25๐
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
- [Django]-Can I use an existing user as Django admin when enabling admin for the first time?
- [Django]-How do I keep my Django server running even after I close my ssh session?
- [Django]-How to convert a Django QuerySet to a list?
11๐
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
- [Django]-Distributed task queues (Ex. Celery) vs crontab scripts
- [Django]-Installing PIL with JPEG support on Mac OS X
- [Django]-How to get the current URL within a Django template?
4๐
Maybe try something suggested here:
data = serializers.serialize("json", models.MyModel.objects.all(), indent=4)
JSON format is easy to print and read ๐
- [Django]-Are sessions needed for python-social-auth
- [Django]-Django 1.10.1 'my_templatetag' is not a registered tag library. Must be one of:
- [Django]-Get Timezone from City in Python/Django
1๐
Inspired by @zeekayโs answer, use the following to see the fields and corresponding values for an instance of a model:
[x for x in Cheese.objects.all()[0].__dict__.items() if not x[0].startswith('_')]
- [Django]-Get list item dynamically in django templates
- [Django]-Custom Filter in Django Admin on Django 1.3 or below
- [Django]-Django urlsafe base64 decoding with decryption
-1๐
Why donโt you implement __unicode__
:
def __unicode__(self):
return self.whatever_field + self.another_field
- [Django]-How to access outermost forloop.counter with nested for loops in Django templates?
- [Django]-Django using get_user_model vs settings.AUTH_USER_MODEL
- [Django]-How do I use an UpdateView to update a Django Model?