Login

JSONField

Author:
deadwisdom
Posted:
August 21, 2007
Language:
Python
Version:
.96
Score:
8 (after 10 ratings)

This is a great way to pack extra data into a model object, where the structure is dynamic, and not relational. For instance, if you wanted to store a list of dictionaries. The data won't be classically searchable, but you can define pretty much any data construct you'd like, as long as it is JSON-serializable. It's especially useful in a JSON heavy application or one that deals with a lot of javascript.

Example (models.py):

from django.db import models
from jsonfield import JSONField

class Sequence(models.Model):
    name = models.CharField(maxlength=25)
    list = JSONField()

Example (shell):

fib = Sequence(name='Fibonacci')
fib.list = [0, 1, 1, 2, 3, 5, 8]
fib.save()

fib = Sequence.objects.get(name='Fibonacci')
fib.list.append(13)
print fib.list
[0, 1, 1, 2, 3, 5, 8, 13]
fib.get_list_json()
"[0, 1, 1, 2, 3, 5, 8, 13]"

Note: You can only save JSON-serializable data. Also, dates will be converted to string-timestamps, because I don't really know what better to do with them. Finally, I'm not sure how to interact with forms yet, so that realm is a bit murky.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import datetime
from django.db import models
from django.db.models import signals
from django.conf import settings
from django.utils import simplejson as json
from django.dispatch import dispatcher

class JSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, datetime.date):
            return obj.strftime('%Y-%m-%d')
        elif isinstance(obj, datetime.time):
            return obj.strftime('%H:%M:%S')
        return json.JSONEncoder.default(self, obj)
        
def dumps(data):
    return JSONEncoder().encode(data)
    
def loads(str):
    return json.loads(str, encoding=settings.DEFAULT_CHARSET)
    
class JSONField(models.TextField):
    def db_type(self):
        return 'text'
        
    def pre_save(self, model_instance, add):
        value = getattr(model_instance, self.attname, None)
        return dumps(value)
    
    def contribute_to_class(self, cls, name):
        super(JSONField, self).contribute_to_class(cls, name)
        dispatcher.connect(self.post_init, signal=signals.post_init, sender=cls)
        
        def get_json(model_instance):
            return dumps(getattr(model_instance, self.attname, None))
        setattr(cls, 'get_%s_json' % self.name, get_json)
    
        def set_json(model_instance, json):
            return setattr(model_instance, self.attname, loads(json))
        setattr(cls, 'set_%s_json' % self.name, set_json)
    
    def post_init(self, instance=None):
        value = self.value_from_object(instance)
        if (value):
            setattr(instance, self.attname, loads(value))
        else:
            setattr(instance, self.attname, None)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

dballanc (on August 29, 2007):

In .96 release I had to add

def get_internal_type(self):
    return 'TextField'

#

newgene (on October 15, 2008):

This JSONField is really handy, but Django's default JsonSerializer will fail to make valid json string from this field. I ends up to add a "value_to_string" method to JSONField:

def value_to_string(self, model_instance):
    value = getattr(model_instance, self.attname, None)
    return dumps(value)

However, Django's JsonSerializer does not pick up this method like XmlSerializer does. I have to create my own JsonSerializer like this one:

from django.core.serializers.json import Serializer as JSONSerializer
from django.utils.encoding import smart_unicode

class Serializer(JSONSerializer):
    def handle_field(self, obj, field):
        if field.value_to_string:
            self._current[field.name] = smart_unicode(field.value_to_string(obj), strings_only=True)
        else:
            self._current[field.name] = smart_unicode(getattr(obj, field.name), strings_only=True)

You need to register this Serializer using "SERIALIZATION_MODULES" in your settings.py.

It works well for me so far.

#

newgene (on October 15, 2008):

There is another related issue I don't know how to fix. If my model contains a JSONField, management command "loaddata" will fail to load the fixture created by "dumpdata", either in json or xml format.

#

X_Y (on September 23, 2012):

With Django 1.3, this code no longer works

#

Please login first before commenting.