From 38c4439877b4551c9197b848b8b4f98b215f659b Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Thu, 31 Mar 2011 11:06:49 -0400 Subject: [PATCH] Resolves issue 68 by treating any incorrectly-passed-in values for JSON fields as json strings rather than as python objects. --- models/fields/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/models/fields/__init__.py b/models/fields/__init__.py index 25af832..1f9603e 100644 --- a/models/fields/__init__.py +++ b/models/fields/__init__.py @@ -51,8 +51,16 @@ class JSONField(models.TextField): models.signals.pre_init.connect(self.fix_init_kwarg, sender=cls) def fix_init_kwarg(self, sender, args, kwargs, **signal_kwargs): + # Anything passed in as self.name is assumed to come from a serializer and + # will be treated as a json string. if self.name in kwargs: - kwargs[self.attname] = json.dumps(kwargs.pop(self.name)) + value = kwargs.pop(self.name) + + # Hack to handle the xml serializer's handling of "null" + if value is None: + value = 'null' + + kwargs[self.attname] = value def formfield(self, *args, **kwargs): kwargs["form_class"] = JSONFormField -- 2.20.1