[Fixed]-Iterating through model fields – Django

24πŸ‘

The _meta attribute on Model classes and instances is a django.db.models.options.Options which provides access to all sorts of useful information about the Model in question.

For fields, it will give you them in the order they were created (i.e. the same order they were declared).

def attrs(self):
    for field in self._meta.fields:
        yield field.name, getattr(self, field.name)

Leave a comment