Commits

Anonymous committed 1db4a18

Fix #39: Don't call convert_value_from_db for fields' default values
(which don't come from the database)

Comments (0)

Files changed (3)

djangotoolbox/db/basecompiler.py

             value = entity.get(field.column, NOT_PROVIDED)
             if value is NOT_PROVIDED:
                 value = field.get_default()
+            else:
+                value = self.convert_value_from_db(field.db_type(connection=self.connection), value)
             if value is None and not field.null:
                 raise IntegrityError("Non-nullable field %s can't be None!" % field.name)
-            value = self.convert_value_from_db(field.db_type(connection=self.connection), value)
             result.append(value)
         return result
 

djangotoolbox/fields.py

 __all__ = ('RawField', 'ListField', 'DictField', 'SetField',
            'BlobField', 'EmbeddedModelField')
 
+EMPTY_ITER = ()
+
 class _HandleAssignment(object):
     """
     A placeholder class that provides a way to set the attribute on the model.
         if item_field is None:
             item_field = RawField()
         self.item_field = item_field
-        default = kwargs.get('default', None if kwargs.get('null') else ())
+        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)

djangotoolbox/tests.py

         self.assertNotEqual(dt, None)
         item.save()
         self.assertGreater(DictModel.objects.get().auto_now['a'], dt)
-        # This shouldn't raise an error becaues the default value is
-        # an empty dict
+        item.delete()
+
+        # Saving empty dicts shouldn't throw errors
         DictModel().save()
+        # Regression tests for djangoappengine issue #39
+        DictModel.add_to_class('new_dict_field', DictField())
+        DictModel.objects.get()
 
     @unittest.skip('Fails with GAE SDK, but passes on production')
     def test_Q_objects(self):