Waldemar Kornewald  committed 2d29b17

fixed support for null=True in fields. thanks to Jonas Haag for the patch

  • Participants
  • Parent commits b1d00ce
  • Branches default

Comments (0)

Files changed (2)

File djangotoolbox/

     def _convert(self, func, values, *args, **kwargs):
         if isinstance(values, (list, tuple, set)):
-            values = [func(value, *args, **kwargs) for value in values]
-            values = self._type(values)
+            return self._type(func(value, *args, **kwargs) for value in values)
         return values
     def to_python(self, value):
         except TypeError:
-            raise ValidationError(_(u'Value of type %r is not iterable' % type(values)))
+            raise ValidationError('Value of type %r is not iterable' % type(values))
     def formfield(self, **kwargs):
-        raise NotImplementedError("No form field implemented for %r" % type(self))
+        raise NotImplementedError('No form field implemented for %r' % type(self))
 class ListField(AbstractIterableField):
     def _convert(self, func, values, *args, **kwargs):
         values = super(ListField, self)._convert(func, values, *args, **kwargs)
-        if self.ordering is not None:
+        if values is not None and self.ordering is not None:
         return values
     _type = dict
     def _convert(self, func, values, *args, **kwargs):
-        return dict([(key, func(values[key], *args, **kwargs))
-                     for key in values])
+        if values is None:
+            return None
+        return dict((key, func(value, *args, **kwargs))
+                     for key, value in values.iteritems())
     def validate(self, values, model_instance):
         if not isinstance(values, dict):
-            raise ValidationError(_(u'Value is of type %r. Should be a dict.' % type(values)))
+            raise ValidationError('Value is of type %r. Should be a dict.' % type(values))
 class BlobField(models.Field):

File djangotoolbox/

 class OrderedListModel(models.Model):
     ordered_ints = ListField(models.IntegerField(max_length=500), default=[],
-                             ordering=lambda x: x)
+                             ordering=lambda x: x, null=True)
+    ordered_nullable = ListField(ordering=lambda x:x, null=True)
 class SetModel(models.Model):
     setfield = SetField(models.IntegerField())
 if supports_dicts:
     class DictModel(models.Model):
         dictfield = DictField(models.IntegerField())
+        dictfield_nullable = DictField(null=True)
 class FilterTest(TestCase):
     floats = [5.3, 2.6, 9.1, 1.58]
         for i, float in enumerate(FilterTest.floats):
             ListModel(floating_point=float, names=FilterTest.names[:i+1]).save()
-    def test_equals_empty(self):
-        self.assertEqual(ListModel.objects.filter(names=[]).count(), 0)
     def test_startswith(self):
         self.assertEquals([entity.names for entity in
         SetModel(setfield=map(str, setdata)).save()
         item = SetModel.objects.filter(setfield=3)[0]
         self.assertEqual(item.setfield, set(setdata))
+        # This shouldn't raise an error because the default value is
+        # an empty list
+        SetModel().save()
     @skip_if(not supports_dicts)
     def test_dictfield(self):
         DictModel(dictfield=dict(a=1, b='55', foo=3.14)).save()
         item = DictModel.objects.get()
         self.assertEqual(item.dictfield, {u'a' : 1, u'b' : 55, u'foo' : 3})
+        # This shouldn't raise an error becaues the default value is
+        # an empty dict
+        DictModel().save()
     # passes on GAE production but not on sdk
     def test_proxy_with_inheritance(self):
-        try:
-            list(ExtendedModelProxy.objects.all())
-        except DatabaseError:
-            pass
-        else:
+        self.assertRaises(DatabaseError, lambda: list(ExtendedModelProxy.objects.all()))