Django — Quick Note From IRC: Model Instance to Dictionary

Truebosko said it well: [530] i must say you are a man of not-so well known django techniques 🙂


>>>from django.forms.models import model_to_dict
>>>model_to_dict(model_instance)
Out:
{'id': 1,
'name': u'joe'
}

etc.

I’m sure i’ll use it in the future.

2 Comments

  1. Yuji says:

    I said I’m sure I’d use it in the future, and lo and behold I did.

    I used it to populate the context of an email via it’s subscribers.

    context=Context(model_to_dict(subscriber_instance))

  2. Tim says:

    This needs to be higher on the google results. All the stack overflow solutions re-implement this badly!

Leave a reply to Tim Cancel reply