Added fix_init_kwarg method to JSONField and connected it to the pre_init signal...
authorStephen Burrows <stephen.r.burrows@gmail.com>
Fri, 14 Jan 2011 19:58:53 +0000 (14:58 -0500)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Fri, 14 Jan 2011 19:58:53 +0000 (14:58 -0500)
models/base.py
models/fields.py

index c7b1c26..fae385a 100644 (file)
@@ -2,8 +2,9 @@ from django import forms
 from django.db import models
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes import generic
 from django.db import models
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes import generic
-from django.utils import simplejson as json
 from django.core.exceptions import ObjectDoesNotExist
 from django.core.exceptions import ObjectDoesNotExist
+from django.utils import simplejson as json
+from django.utils.encoding import smart_str
 from philo.exceptions import AncestorDoesNotExist
 from philo.models.fields import JSONField
 from philo.utils import ContentTypeRegistryLimiter, ContentTypeSubclassLimiter
 from philo.exceptions import AncestorDoesNotExist
 from philo.models.fields import JSONField
 from philo.utils import ContentTypeRegistryLimiter, ContentTypeSubclassLimiter
@@ -73,7 +74,7 @@ class JSONValue(AttributeValue):
        value = JSONField() #verbose_name='Value (JSON)', help_text='This value must be valid JSON.')
        
        def __unicode__(self):
        value = JSONField() #verbose_name='Value (JSON)', help_text='This value must be valid JSON.')
        
        def __unicode__(self):
-               return self.value_json
+               return smart_str(self.value)
        
        def value_formfield(self, **kwargs):
                kwargs['initial'] = self.value_json
        
        def value_formfield(self, **kwargs):
                kwargs['initial'] = self.value_json
index 19a6006..83798c4 100644 (file)
@@ -243,6 +243,11 @@ class JSONField(models.TextField):
        def contribute_to_class(self, cls, name):
                super(JSONField, self).contribute_to_class(cls, name)
                setattr(cls, name, JSONDescriptor(self))
        def contribute_to_class(self, cls, name):
                super(JSONField, self).contribute_to_class(cls, name)
                setattr(cls, name, JSONDescriptor(self))
+               models.signals.pre_init.connect(self.fix_init_kwarg, sender=cls)
+       
+       def fix_init_kwarg(self, sender, args, kwargs, **signal_kwargs):
+               if self.name in kwargs:
+                       kwargs[self.attname] = json.dumps(kwargs.pop(self.name))
        
        def formfield(self, *args, **kwargs):
                kwargs["form_class"] = JSONFormField
        
        def formfield(self, *args, **kwargs):
                kwargs["form_class"] = JSONFormField