Commits

Waldemar Kornewald committed fc01792

fixed ForeignKey support in embedded models. thanks for the patch Jonas Haag!

Comments (0)

Files changed (2)

djangotoolbox/fields.py

         values = dict()
         for name, value in embedded_dict.iteritems():
             field = embedded_instance._meta.get_field(name)
-            values[name] =  field.get_db_prep_value(value, **kwargs)
+            values[field.attname] =  field.get_db_prep_value(value, **kwargs)
         if self.embedded_model is None:
             values.update({'_module' : embedded_instance.__class__.__module__,
                            '_model'  : embedded_instance.__class__.__name__})

djangotoolbox/tests.py

         untyped_dict = DictField(EmbeddedModelField())
 
     class EmbeddedModel(models.Model):
+        some_relation = models.ForeignKey(DictModel, null=True)
         someint = models.IntegerField()
         auto_now = models.DateTimeField(auto_now=True)
         auto_now_add = models.DateTimeField(auto_now_add=True)
         data = EmbeddedModelFieldModel.objects.get().untyped_dict
         self.assertIsInstance(data['a'], SetModel)
         self.assertNotEqual(data['c'].auto_now['y'], None)
+
+    def test_foreignkey_in_embedded_object(self):
+        simple = EmbeddedModel(some_relation=DictModel.objects.create())
+        obj = EmbeddedModelFieldModel.objects.create(simple=simple)
+        simple = EmbeddedModelFieldModel.objects.get().simple
+        self.assertNotIn('some_relation', simple.__dict__)
+        self.assertIsInstance(simple.__dict__['some_relation_id'], type(obj.id))
+        self.assertIsInstance(simple.some_relation, DictModel)
 EmbeddedModelFieldTest = unittest.skipIf(
     not supports_dicts, "Backend doesn't support dicts")(
     EmbeddedModelFieldTest)