From: Matías Aguirre Date: Tue, 15 Nov 2011 16:32:33 +0000 (-0200) Subject: Fix JSONField rendering on admin section. Closes gh-176 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=7ae03e087298e52a9db9655d450af4c3fd82efc3;p=django-social-auth.git Fix JSONField rendering on admin section. Closes gh-176 --- diff --git a/social_auth/fields.py b/social_auth/fields.py index 73cf386..3052360 100644 --- a/social_auth/fields.py +++ b/social_auth/fields.py @@ -8,7 +8,6 @@ class JSONField(models.TextField): """Simple JSON field that stores python structures as JSON strings on database. """ - __metaclass__ = models.SubfieldBase def to_python(self, value): @@ -22,30 +21,35 @@ class JSONField(models.TextField): try: return simplejson.loads(value) except Exception, e: - raise ValidationError(str(e)) + raise ValidationError(e.message) else: return value def validate(self, value, model_instance): """Check value is a valid JSON string, raise ValidationError on error.""" - super(JSONField, self).validate(value, model_instance) - try: - return simplejson.loads(value) - except Exception, e: - raise ValidationError(str(e)) + if isinstance(value, basestring): + super(JSONField, self).validate(value, model_instance) + try: + simplejson.loads(value) + except Exception, e: + raise ValidationError(e.message) def get_prep_value(self, value): """Convert value to JSON string before save""" try: return simplejson.dumps(value) except Exception, e: - raise ValidationError(str(e)) + raise ValidationError(e.message) def value_to_string(self, obj): """Return value from object converted to string properly""" return smart_unicode(self.get_prep_value(self._get_val_from_obj(obj))) + def value_from_object(self, obj): + """Return value dumped to string.""" + return self.get_prep_value(self._get_val_from_obj(obj)) + try: from south.modelsinspector import add_introspection_rules