Matthew Schinckel avatar Matthew Schinckel committed 86ba1f3

Prevent deprecation warnings about calling get_db_prep_value withou connection argument.
Do better value checking on initialisation.

Comments (0)

Files changed (2)

jsonfield/fields.py

     }
     description = "JSON object"
     
-    def contribute_to_class(self, cls, name):
-        assert self.null or self.blank or self.default not in ['', models.fields.NOT_PROVIDED], \
-            "JSONField '%s' in '%s' must contain one of null=True, blank=True or non-empty string default." % (
-                name, cls.__name__
-            )
-        super(JSONField, self).contribute_to_class(cls, name)
+    def __init__(self, *args, **kwargs):
+        if not kwargs.get('null', False):
+            kwargs['default'] = kwargs.get('default', {})
+        super(JSONField, self).__init__(*args, **kwargs)
+        if 'default' in kwargs:
+            if callable(self.default):
+                self.validate(self.default(), None)
+            else:
+                self.validate(self.default, None)
         
     def formfield(self, **kwargs):
         return super(JSONField, self).formfield(form_class=JSONFormField, **kwargs)
         return value
 
     def get_db_prep_value(self, value, connection=None, prepared=None):
+        return self.get_db_prep_value(value)
+    
+    def get_prep_value(self, value):
         if value is None:
             if not self.null and self.blank:
                 return ""
     
     def get_prep_lookup(self, lookup_type, value):
         if lookup_type in ["exact", "iexact"]:
-            return self.to_python(self.get_db_prep_value(value))
+            return self.to_python(self.get_prep_value(value))
         if lookup_type == "in":
-            return [self.to_python(self.get_db_prep_value(v)) for v in value]
+            return [self.to_python(self.get_prep_value(v)) for v in value]
         if lookup_type == "isnull":
             return value
         if lookup_type in ["contains", "icontains"]:
                     lookup_type, type(value).__name__
                 ))
                 # Need a way co combine the values with '%', but don't escape that.
-                return self.get_db_prep_value(value)[1:-1].replace(', ', r'%')
+                return self.get_prep_value(value)[1:-1].replace(', ', r'%')
             if isinstance(value, dict):
-                return self.get_db_prep_value(value)[1:-1]
-            return self.to_python(self.get_db_prep_value(value))
+                return self.get_prep_value(value)[1:-1]
+            return self.to_python(self.get_prep_value(value))
         raise TypeError('Lookup type %r not supported' % lookup_type)
 
     def value_to_string(self, obj):

jsonfield/tests/base.py

         self.assertEquals("", obj.blank_json)
     
     def test_invalid_default_value(self):
-        def make_model():
+        def no_default():
             class InvalidFieldModel(models.Model):
                 json = JSONField()
                 class Meta:
                     app_label = 'jsonfield'
         
-        self.assertRaises(AssertionError, make_model)
-        
+        # self.assertRaises(AssertionError, no_default)
+        
+        def bad_default():
+            class InvalidFieldModel(models.Model):
+                json = JSONField(default="foo")
+                class Meta:
+                    app_label = 'jsonfield'
+                
+        self.assertRaises(AssertionError, bad_default)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.