Django: Converting an entire Model into a single dictionary
- by LarrikJ
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.