Commits

Anonymous committed 675c523

Fix #9: Allow field classes to be passed to fields (not only field instances)

Comments (0)

Files changed (2)

djangotoolbox/fields.py

     appropriate data type.
     """
     def __init__(self, item_field=None, *args, **kwargs):
-        if item_field is None:
-            item_field = RawField()
-        self.item_field = item_field
         default = kwargs.get('default', None if kwargs.get('null') else EMPTY_ITER)
         if default is not None and not callable(default):
             # ensure a new object is created every time the default is accessed
             kwargs['default'] = lambda: self._type(default)
+
         super(AbstractIterableField, self).__init__(*args, **kwargs)
 
+        if item_field is None:
+            item_field = RawField()
+        elif callable(item_field):
+            item_field = item_field()
+        self.item_field = item_field
+
     def contribute_to_class(self, cls, name):
         self.item_field.model = cls
         self.item_field.name = name

djangotoolbox/tests.py

 class ListModel(models.Model):
     integer = models.IntegerField(primary_key=True)
     floating_point = models.FloatField()
-    names = ListField(models.CharField(max_length=500))
+    names = ListField(models.CharField)
     names_with_default = ListField(models.CharField(max_length=500), default=[])
     names_nullable = ListField(models.CharField(max_length=500), null=True)
 
 supports_dicts = getattr(connections['default'].features, 'supports_dicts', False)
 if supports_dicts:
     class DictModel(models.Model):
-        dictfield = DictField(models.IntegerField())
+        dictfield = DictField(models.IntegerField)
         dictfield_nullable = DictField(null=True)
         auto_now = DictField(models.DateTimeField(auto_now=True))