Matthew Schinckel  committed 067042d

Refuse to allow adding a field to a class if it is invalid.
That is, check that either null=True, blank=True, or a valid default is
defined for that field.

  • Participants
  • Parent commits aff86d5

Comments (0)

Files changed (2)

File jsonfield/

     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 formfield(self, **kwargs):
         return super(JSONField, self).formfield(form_class=JSONFormField, **kwargs)

File jsonfield/tests/

     def test_value_to_string(self):
+        """
+        This is a failure because I'm not sure that value_to_string is the
+        right name for this method in django: what we are really doing is
+        preparing for serialisation. Since we want to embed the JSON object
+        in our other JSON structure, in my case, anyway, I actually want
+        this to return the python object, not a JSON serialisation of it!
+        """
         field = JSONField(u"test")
         obj = JSONFieldTestModel(json='''{
         obj = BlankJSONFieldTestModel.objects.get()
         self.assertEquals(None, obj.null_json)
         self.assertEquals("", obj.blank_json)
+    def test_invalid_default_value(self):
+        def make_model():
+            class InvalidFieldModel(models.Model):
+                json = JSONField()
+                class Meta:
+                    app_label = 'jsonfield'
+        self.assertRaises(AssertionError, make_model)