JSONField

 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. JSONField by Jasber 4 years, 11 months ago
  2. Custom model field to store dict object in database by rudyryk 4 years ago
  3. Pattern to integer list function by marinho 6 years, 4 months ago
  4. Integer list to pattern function by marinho 6 years, 4 months ago
  5. Javascript Chain Select Widget by ogo 5 years, 10 months 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

#

(Forgotten your password?)