class JSONField(models.TextField):
    """Specialized text field that holds JSON in the database, which is
    represented within Python as (usually) a dictionary."""
__metaclass__ = models.SubfieldBase

def __init__(self, blank=True, default='{}', help_text=
    'Specialized text field that holds JSON in the database, which is represented within Python as (usually) a dictionary.'
    , *args, **kwargs):
    super(JSONField, self).__init__(blank=blank, default=default, help_text
        =help_text, *args, **kwargs)

def get_prep_value(self, value):
    if type(value) in (str, str) and len(value) == 0:
        value = None
return json.dumps(value)

def formfield(self, form_class=JSONFormField, **kwargs):
    return super(JSONField, self).formfield(form_class=form_class, **kwargs)

def bound_data(self, data, initial):
    return json.dumps(data)

def to_python(self, value):
    if type(value) not in (str, str):
        return value
if len(value) == 0:
    return None
if value == 'NaN':
    return None
if value not in ('true', 'false', 'null') and (value[0] not in ('{', '[',
    '"') or value[-1] not in ('}', ']', '"')):
    return value
return json.loads(value)
Most elegant approach for writing JSON data to a relational database using Django Models?