26👍
You can loop over all field names like so
for name in Company._meta.get_all_field_names():
print name
this also works if you have a category instance:
c = Company(name="foo",website="bar",email="baz@qux.com",....,)
c.save()
for field in c._meta.get_all_field_names():
print getattr(c, field, None)
Update for Django 1.8
Django 1.8 now has an official model Meta api and you can easily grab all the fields:
from django.contrib.auth.models import User
for field in User._meta.get_fields():
print field
- Running django tests with selenium in docker
- Django dynamic Form example
- Got AttributeError when attempting to get a value for field on serializer
- Django and reverse relations from foreign keys
- Add method imports to shell_plus
7👍
Iteration in a view:
If you have an instance company
of Company
:
fields= company.__dict__
for field, value in fields.items():
print field, value
👤rom
- Celery chaining tasks sequentially
- Token authentication does not work in production on django rest framework
- How to use a tsvector field to perform ranking in Django with postgresql full-text search?
- Query a template for the variables it needs?
1👍
this is a possible solution:
entity = Company.all().get()
for propname, prop in entity.properties().items():
print propname, prop.get_value_for_datastore(entity)
another one could be:
# this returns a dict with the property
# name as key and the property val as its value
entity.__dict__.get('_entity')
- Is a varchar 2 more efficient than a varchar 255?
- Is there a Python3 compatible Django storage backend for Amazon S3?
- Strategies to make a web application available offline?
Source:stackexchange.com