fromdjango.dbimportmodelsfromdjango.core.serializers.jsonimportDjangoJSONEncoderfromdjango.utilsimportsimplejsonasjsonclassJSONField(models.TextField):"""JSONField is a generic textfield that neatly serializes/unserializes JSON objects seamlessly"""# Used so to_python() is called__metaclass__=models.SubfieldBasedefto_python(self,value):"""Convert our string value to JSON after we load it from the DB"""ifvalue=="":returnNonetry:ifisinstance(value,basestring):returnjson.loads(value)exceptValueError:passreturnvaluedefget_db_prep_save(self,value):"""Convert our JSON object to a string before we save"""ifvalue=="":returnNoneifisinstance(value,dict):value=json.dumps(value,cls=DjangoJSONEncoder)returnsuper(JSONField,self).get_db_prep_save(value)

This worked a little better for me. Though I am still not all that happy with it.

# # # # # # # # # # # # # # # # #
from django.db import models
from django.core.serializers.json import DjangoJSONEncoder
from django.utils import simplejson as json
class JSONField(models.TextField):
"""JSONField is a generic textfield that neatly serializes/unserializes
JSON objects seamlessly
The json spec claims you must use a collection type at the top level of
the data structure. However the simplesjon decoder and Firefox both encode
and decode non collection types that do not exist inside a collection.
The to_python method relies on the value being an instance of basestring
to ensure that it is encoded. If a string is the sole value at the
point the field is instanced, to_python attempts to decode the sting because
it is derived from basestring but cannot be encodeded and throws the
exception ValueError: No JSON object could be decoded.
"""
# Used so to_python() is called
__metaclass__ = models.SubfieldBase
def to_python(self, value):
"""Convert our string value to JSON after we load it from the DB"""
if value == "":
return None
try:
if isinstance(value, basestring):
return json.loads(value)
except ValueError:
return value
return value
def get_db_prep_save(self, value):
"""Convert our JSON object to a string before we save"""
if value == "":
return None
return super(JSONField, self).get_db_prep_save(json.dumps(value, cls=DjangoJSONEncoder))

I was being thrown for a loop with e.g. "Error binding parameter" (using sqlite) when trying to save a model object that had a JSONField. Turns out I was passing in a list, not a dict (valid JSON), and this JSONField doesn't handle lists. The quick fix has line 32 become:

modified it, so it can hold lists and does serialize (at least i tried json serialization)
also modified after the docs to use get_prep_value, since they write its the function to use as antidote to to_python.