class JSONField(models.TextField):
    """
    JSONField is a generic textfield that neatly serializes/unserializes
    JSON objects seamlessly.
    Django snippet 
    example:
        class Page(models.Model):
            data = JSONField(blank=True, null=True)
        page = Page.objects.get(pk=5)
        page.data = {'title': 'test', 'type': 3}
        page.save()
    """
__metaclass__ = models.SubfieldBase

def to_python(self, value):
    if value == '':
        return None
try:
    if isinstance(value, str):
        return json.loads(value)
except ValueError:
    pass
return value

def get_db_prep_save(self, value, *args, **kwargs):
    if value == '':
        return None
if isinstance(value, dict):
    value = json.dumps(value, cls=DjangoJSONEncoder)
return super(JSONField, self).get_db_prep_save(value, *args, **kwargs)
Как сохранить словарь в поле модели базы данных Django