Django: Converting an entire Model into a single dictionary

Posted by LarrikJ on Stack Overflow See other posts from Stack Overflow or by LarrikJ
Published on 2009-07-14T03:35:53Z Indexed on 2010/03/15 22:19 UTC
Read the original article Hit count: 129

Is there a good way in Django to convert an entire model to a dictionary? I mean, like this:

class DictModel(models.Model):
    key = models.CharField(20)
    value = models.CharField(200)


DictModel.objects.all().to_dict()

... with the result being a dictionary with the key/value pairs made up of records in the Model? Has anyone else seen this as being useful for them?

Thanks.

Update
I just wanted to add is that my ultimate goal is to be able to do a simple variable lookup inside a Template. Something like:

{{ DictModel.exampleKey }}

With a result of DictModel.objects.get(key__exact=exampleKey).value

Overall, though, you guys have really surprised me with how helpful allof your responses are, and how different the ways to approach it can be. Thanks a lot.

© Stack Overflow or respective owner

Related posts about django

Related posts about python