]> git.parisson.com Git - django-social-auth.git/commitdiff
Fix JSONField rendering on admin section. Closes gh-176
authorMatías Aguirre <matiasaguirre@gmail.com>
Tue, 15 Nov 2011 16:32:33 +0000 (14:32 -0200)
committerMatías Aguirre <matiasaguirre@gmail.com>
Tue, 15 Nov 2011 16:32:33 +0000 (14:32 -0200)
social_auth/fields.py

index 73cf3868c4370cd4ac219a9bd90ef86ddc9eb64a..3052360955af4de233e2d66ebc6b102bf6c4d671 100644 (file)
@@ -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