Permalink
Browse files

Merge pull request #22 from l0kix2/master

Pretty-print when using ModelForm.
  • Loading branch information...
2 parents 3bccdc4 + e1ab208 commit 4cbd66fe18fdbaa25a631f8c409dec1ecb78e1a2 @bradjasper committed Nov 13, 2012
Showing with 13 additions and 2 deletions.
  1. +2 −1 .gitignore
  2. +11 −1 jsonfield/fields.py
View
@@ -4,4 +4,5 @@ dist/
MANIFEST
.project
.pydevproject
-.DS_Store
+.DS_Store
+.idea/*
View
@@ -48,14 +48,22 @@ def get_db_prep_value(self, value, connection, prepared=False):
if isinstance(value, basestring):
return value
+ if self.null and value is None:
+ return None
return json.dumps(value, **self.dump_kwargs)
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return self.get_prep_value(value)
def value_from_object(self, obj):
- return json.dumps(super(JSONFieldBase, self).value_from_object(obj))
+ value = super(JSONFieldBase, self).value_from_object(obj)
+ if self.null and value is None:
+ return None
+ return self.dumps_for_display(value)
+
+ def dumps_for_display(self, value):
+ return json.dumps(value)
def formfield(self, **kwargs):
@@ -72,6 +80,8 @@ def formfield(self, **kwargs):
class JSONField(JSONFieldBase, models.TextField):
"""JSONField is a generic textfield that serializes/unserializes JSON objects"""
+ def dumps_for_display(self, value):
+ return json.dumps(value, indent=2)
class JSONCharField(JSONFieldBase, models.CharField):

0 comments on commit 4cbd66f

Please sign in to comment.