Waldemar Kornewald avatar Waldemar Kornewald committed 41ec43a

renamed GenericField to RawField to make it clearer what it does (i.e., you can pass any low-level datatype in there and it'll be stored without any field-specific conversion, so not all field-specific types might be supported: e.g. GAE doesn't support date and time objects, only datetime)

Comments (0)

Files changed (3)

djangotoolbox/db/creation.py

     data_types = {
         'AutoField':         'integer',
         'BigIntegerField':   'long',
-        'BlobField':         'blob',
         'BooleanField':      'bool',
         'CharField':         'text',
         'CommaSeparatedIntegerField': 'text',
         'TimeField':         'time',
         'URLField':          'text',
         'XMLField':          'longtext',
+
+        'BlobField':         'blob',
+        'RawField':          'raw',
     }

djangotoolbox/fields.py

 
 __all__ = ('GenericField', 'ListField', 'DictField', 'SetField', 'BlobField')
 
-class GenericField(models.Field):
+class RawField(models.Field):
     """ Generic field to store anything your database backend allows you to. """
-    pass
+    def get_internal_type(self):
+        return 'RawField'
 
 class AbstractIterableField(models.Field):
     """
     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 ())
         if default is not None and not callable(default):
 
     In the latter case, the object has to provide a ``read`` method from which
     the blob is read.
-
-    If the optional keyword arguments `close_files` is ``True``, the ``close``
-    method of file-like values will be called after ``read``ing the contents
-    (if such a method exists at all).
     """
-    def __init__(self, *args, **kwargs):
-        self.close_files = kwargs.pop('close_files', False)
-        super(BlobField, self).__init__(*args, **kwargs)
-
     def get_internal_type(self):
         return 'BlobField'
 
 
     def get_db_prep_value(self, value, connection, prepared=False):
         if hasattr(value, 'read'):
-            content = value.read()
-            if self.close_files and hasattr(value, 'close'):
-                value.close()
+            return value.read()
         else:
             return str(value)
 

djangotoolbox/tests.py

     names_with_default = ListField(models.CharField(max_length=500), default=[])
     names_nullable = ListField(models.CharField(max_length=500), null=True)
 
+class OrderedListModel(models.Model):
+    ordered_ints = ListField(models.IntegerField(max_length=500), default=[],
+                             ordering=lambda x: x)
+
 class SetModel(models.Model):
     setfield = SetField(models.IntegerField())
 
 class FilterTest(TestCase):
     floats = [5.3, 2.6, 9.1, 1.58]
     names = [u'Kakashi', u'Naruto', u'Sasuke', u'Sakura',]
+    unordered_ints = [4, 2, 6, 1]
 
     def setUp(self):
         for i, float in enumerate(FilterTest.floats):
                             ['Kakashi', 'Naruto', 'Sasuke', 'Sakura',]])
 
     def test_options(self):
-        self.assertEquals([entity.names_with_default for entity in
+        self.assertEqual([entity.names_with_default for entity in
                            ListModel.objects.filter(names__startswith='Sa')],
                           [[], []])
 
-        # TODO: should it be NULL or None here?
-        self.assertEquals([entity.names_nullable for entity in
+        self.assertEqual([entity.names_nullable for entity in
                            ListModel.objects.filter(names__startswith='Sa')],
                           [None, None])
 
+    def test_default_value(self):
+        # Make sure default value is copied
+        ListModel().names_with_default.append(2)
+        self.assertEqual(ListModel().names_with_default, [])
+
+    def test_ordering(self):
+        OrderedListModel(ordered_ints=self.unordered_ints).save()
+        self.assertEqual(OrderedListModel.objects.get().ordered_ints,
+                         sorted(self.unordered_ints))
+
     def test_gt(self):
         # test gt on list
         self.assertEquals([entity.names for entity in
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.