From 0e3cdffac41250cddfe93388b1c9fc1547e77a67 Mon Sep 17 00:00:00 2001 From: Raphaƫl Barrois Date: Sat, 25 Apr 2015 17:50:50 +0200 Subject: Clarify .build() issue with Django>1.8 (Ref #198). From 1.8 onwards, this crashes: >>> a = MyModel() # Don't save >>> b = MyOtherModel(fkey_to_mymodel=a) In turn, it breaks: class MyModelFactory(factory.django.DjangoModelFactory): class Meta: model = MyModel class MyOtherModelFactory(factory.django.DjangoModelFactory): class Meta: model = MyOtherModel fkey_to_mymodel = factory.SubFactory(MyModelFactory) MyOtherModelFactory.build() # Breaks The error message is: Cannot assign "MyModel()": "MyModel" instance isn't saved in the database. See https://code.djangoproject.com/ticket/10811 for details. --- docs/orms.rst | 8 ++++++++ tests/djapp/models.py | 9 +++++++++ tests/test_django.py | 26 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/docs/orms.rst b/docs/orms.rst index 5105e66..bbe91e6 100644 --- a/docs/orms.rst +++ b/docs/orms.rst @@ -40,6 +40,14 @@ All factories for a Django :class:`~django.db.models.Model` should use the once all post-generation hooks have run. +.. note:: Starting with Django 1.8, it is no longer possible to call ``.build()`` + on a factory if this factory uses a :class:`~factory.SubFactory` pointing + to another model: Django refuses to set a :class:`~djang.db.models.ForeignKey` + to an unsaved :class:`~django.db.models.Model` instance. + + See https://code.djangoproject.com/ticket/10811 for details. + + .. class:: DjangoOptions(factory.base.FactoryOptions) The ``class Meta`` on a :class:`~DjangoModelFactory` supports extra parameters: diff --git a/tests/djapp/models.py b/tests/djapp/models.py index 513c47c..68b9709 100644 --- a/tests/djapp/models.py +++ b/tests/djapp/models.py @@ -68,6 +68,15 @@ class StandardSon(StandardModel): pass +class PointedModel(models.Model): + foo = models.CharField(max_length=20) + + +class PointingModel(models.Model): + foo = models.CharField(max_length=20) + pointed = models.OneToOneField(PointedModel, related_name='pointer', null=True) + + WITHFILE_UPLOAD_TO = 'django' WITHFILE_UPLOAD_DIR = os.path.join(settings.MEDIA_ROOT, WITHFILE_UPLOAD_TO) diff --git a/tests/test_django.py b/tests/test_django.py index 113caeb..33d159d 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -361,6 +361,32 @@ class DjangoAbstractBaseSequenceTestCase(django_test.TestCase): self.assertEqual(1, obj.pk) +@unittest.skipIf(django is None, "Django not installed.") +class DjangoRelatedFieldTestCase(django_test.TestCase): + + @classmethod + def setUpClass(cls): + super(DjangoRelatedFieldTestCase, cls).setUpClass() + class PointedFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.PointedModel + foo = 'ahah' + + class PointerFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.PointingModel + pointed = factory.SubFactory(PointedFactory, foo='hihi') + foo = 'bar' + + cls.PointedFactory = PointedFactory + cls.PointerFactory = PointerFactory + + def test_direct_related_create(self): + ptr = self.PointerFactory() + self.assertEqual('hihi', ptr.pointed.foo) + self.assertEqual(ptr.pointed, models.PointedModel.objects.get()) + + @unittest.skipIf(django is None, "Django not installed.") class DjangoFileFieldTestCase(unittest.TestCase): -- cgit v1.2.3