How to store a dictionary on a Django Model? How to store a dictionary on a Django Model? python python

How to store a dictionary on a Django Model?


If it's really dictionary like arbitrary data you're looking for you can probably use a two-level setup with one model that's a container and another model that's key-value pairs. You'd create an instance of the container, create each of the key-value instances, and associate the set of key-value instances with the container instance. Something like:

class Dicty(models.Model):    name      = models.CharField(max_length=50)class KeyVal(models.Model):    container = models.ForeignKey(Dicty, db_index=True)    key       = models.CharField(max_length=240, db_index=True)    value     = models.CharField(max_length=240, db_index=True)

It's not pretty, but it'll let you access/search the innards of the dictionary using the DB whereas a pickle/serialize solution will not.


If you don't need to query by any of this extra data, then you can store it as a serialized dictionary. Use repr to turn the dictionary into a string, and eval to turn the string back into a dictionary. Take care with eval that there's no user data in the dictionary, or use a safe_eval implementation.

For example, in the create and update methods of your views, you can add:

if isinstance(request.data, dict) == False:    req_data = request.data.dict().copy()else:    req_data = request.data.copy()dict_key = 'request_parameter_that_has_a_dict_inside'if dict_key in req_data.keys() and isinstance(req_data[dict_key], dict):    req_data[dict_key] = repr(req_data[dict_key])


Another clean and fast solution can be found here: https://github.com/bradjasper/django-jsonfield

For convenience I copied the simple instructions.

Install

pip install jsonfield

Usage

from django.db import modelsfrom jsonfield import JSONFieldclass MyModel(models.Model):    json = JSONField()