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.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
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 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