Commits

Matthew Schinckel  committed 462876e

Don't inherit from TextField: otherwise admin overrides our widget.

  • Participants
  • Parent commits c57f5d6

Comments (0)

Files changed (5)

File jsonfield/fields.py

 from decimal import Decimal
 import datetime
 
+from utils import default
+from widgets import JSONWidget
 from forms import JSONFormField
 
-def default(o):
-    if hasattr(o, 'to_json'):
-        return o.to_json()
-    if isinstance(o, Decimal):
-        return str(o)
-    if isinstance(o, datetime.datetime):
-        return o.strftime("%Y-%m-%dT%H:%M:%S")
-    if isinstance(o, datetime.date):
-        return o.strftime("%Y-%m-%d")
-    if isinstance(o, datetime.time):
-        return o.strftime("%H:%M:%S")
-
-    raise TypeError(repr(o) + " is not JSON serializable")
-
-
-class JSONField(models.TextField):
+class JSONField(models.Field):
     """
     A field that will ensure the data entered into it is valid JSON.
     """
     default_error_messages = {
         'invalid': _(u"'%s' is not a valid JSON string.")
     }
-    description = "JSON object"
+    description = _("JSON object")
     
     def __init__(self, *args, **kwargs):
         if not kwargs.get('null', False):
                 self.validate(self.default, None)
         
     def formfield(self, **kwargs):
-        return super(JSONField, self).formfield(form_class=JSONFormField, **kwargs)
+        defaults = {
+            'form_class': JSONFormField,
+            'widget': JSONWidget
+        }
+        defaults.update(**kwargs)
+        return super(JSONField, self).formfield(**defaults)
     
     def validate(self, value, model_instance):
         if not self.null and value is None:

File jsonfield/tests/base.py

 from django.utils import unittest
 
 from jsonfield.tests.jsonfield_test_app.models import *
-from jsonfield import JSONField
+from jsonfield.fields import JSONField
 
 class JSONFieldTest(DjangoTestCase):
     def test_json_field(self):

File jsonfield/utils.py

 import datetime
+from decimal import Decimal
+
 from django.core.serializers.json import DjangoJSONEncoder
 
 class TZAwareJSONEncoder(DjangoJSONEncoder):
     def default(self, obj):
         if isinstance(obj, datetime.datetime):
             return obj.strftime("%Y-%m-%d %H:%M:%S%z")
-        return super(TZAwareJSONEncoder, self).default(obj)
+        return super(TZAwareJSONEncoder, self).default(obj)
+
+def default(o):
+    if hasattr(o, 'to_json'):
+        return o.to_json()
+    if isinstance(o, Decimal):
+        return str(o)
+    if isinstance(o, datetime.datetime):
+        return o.strftime("%Y-%m-%dT%H:%M:%S")
+    if isinstance(o, datetime.date):
+        return o.strftime("%Y-%m-%d")
+    if isinstance(o, datetime.time):
+        return o.strftime("%H:%M:%S")
+
+    raise TypeError(repr(o) + " is not JSON serializable")

File jsonfield/widgets.py

 from django.utils import simplejson as json
 from django.conf import settings
 
-import jsonfield.fields
+from utils import default
 
 class JSONWidget(forms.Textarea):
     def render(self, name, value, attrs=None):
         if value is None:
             value = ""
         if not isinstance(value, basestring):
-            value = json.dumps(value, indent=2, default=jsonfield.fields.default)
+            value = json.dumps(value, indent=2, default=default)
         return super(JSONWidget, self).render(name, value, attrs)
 
 
 class JSONSelectWidget(forms.SelectMultiple):
     pass
+
     global_settings.STATIC_URL = "/static/"
     global_settings.MEDIA_ROOT = os.path.join(BASE_PATH, 'static')
     global_settings.STATIC_ROOT = global_settings.MEDIA_ROOT
-
+    
     from django.test.utils import get_runner
     test_runner = get_runner(global_settings)