Commits

Waldemar Kornewald  committed 30c36f5

added better handling for ListField's default value and minor cleanups

  • Participants
  • Parent commits 886c51c

Comments (0)

Files changed (3)

File djangotoolbox/db/basecompiler.py

 
     def get_fields(self):
         """
-        Returns the fields which should get loaded from the backend by self.query        
+        Returns the fields which should get loaded from the backend by self.query
         """
         # We only set this up here because
         # related_select_fields isn't populated until

File djangotoolbox/db/creation.py

 
 class NonrelDatabaseCreation(BaseDatabaseCreation):
     data_types = {
-        'DateTimeField':     'datetime',
-        'DateField':         'date',
-        'TimeField':         'time',
-        'FloatField':        'float',
-        'EmailField':        'text',
-        'URLField':          'text',
+        'AutoField':         'integer',
+        'BigIntegerField':   'long',
+        'BlobField':         'blob',
         'BooleanField':      'bool',
-        'NullBooleanField':  'bool',
         'CharField':         'text',
         'CommaSeparatedIntegerField': 'text',
-        'IPAddressField':    'text',
-        'SlugField':         'text',
+        'DateField':         'date',
+        'DateTimeField':     'datetime',
+        'DecimalField':      'decimal:%(max_digits)s,%(decimal_places)s',
+        'EmailField':        'text',
         'FileField':         'text',
         'FilePathField':     'text',
-        'TextField':         'longtext',
-        'XMLField':          'longtext',
+        'FloatField':        'float',
+        'ImageField':        'text',
         'IntegerField':      'integer',
-        'SmallIntegerField': 'integer',
+        'IPAddressField':    'text',
+        'NullBooleanField':  'bool',
+        'OneToOneField':     'integer',
         'PositiveIntegerField': 'integer',
         'PositiveSmallIntegerField': 'integer',
-        'BigIntegerField':   'long',
-        'AutoField':         'integer',
-        'OneToOneField':     'integer',
-        'DecimalField':      'decimal:%(max_digits)s,%(decimal_places)s',
-        'BlobField':         'blob',
-#        'ImageField':
+        'SlugField':         'text',
+        'SmallIntegerField': 'integer',
+        'TextField':         'longtext',
+        'TimeField':         'time',
+        'URLField':          'text',
+        'XMLField':          'longtext',
     }

File djangotoolbox/fields.py

 class ListField(models.Field):
     def __init__(self, field_type, *args, **kwargs):
         self.field_type = field_type
-        kwargs['default'] = lambda: None if self.null else []
+        kwargs.setdefault('default', lambda: None if self.null else [])
+        if not callable(kwargs['default']):
+            default = kwargs['default']
+            kwargs['default'] = lambda: default[:]
         super(ListField, self).__init__(*args, **kwargs)
 
+    def contribute_to_class(self, cls, name):
+        self.field_type.model = cls
+        self.field_type.name = name
+        super(ListField, self).contribute_to_class(cls, name)
+
     def db_type(self, connection):
         return 'ListField:' + self.field_type.db_type(connection=connection)
 
         return values
 
     def to_python(self, value):
-        return self.call_for_each( 'to_python', value)
+        return self.call_for_each('to_python', value)
 
     def get_prep_value(self, value):
         return self.call_for_each('get_prep_value', value)
         return 'BlobField'
 
     def formfield(self, **kwargs):
-        # A file widget is provided, but use  model FileField or ImageField 
+        # A file widget is provided, but use  model FileField or ImageField
         # for storing specific files most of the time
         from .widgets import BlobWidget
         from django.forms import FileField
         defaults.update(kwargs)
         return super(BlobField, self).formfield(**defaults)
 
-    def get_db_prep_value(self, value, connection, prepared=False):        
+    def get_db_prep_value(self, value, connection, prepared=False):
         try:
             # Sees if the object passed in is file-like
             return value.read()