From cfa5fa47b02699b10560295d6003edb00d60092b Mon Sep 17 00:00:00 2001 From: Andrew Plummer Date: Wed, 2 Mar 2016 17:15:13 +0000 Subject: [PATCH] Test to show how to do FKs between apps with South --- tests/test_second_app/tests.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/test_second_app/tests.py b/tests/test_second_app/tests.py index 3483b3a..53b8028 100644 --- a/tests/test_second_app/tests.py +++ b/tests/test_second_app/tests.py @@ -73,13 +73,15 @@ def test_both_migrated(self): self.assertEqual(mymodel.number, 10) -# TODO: conclude if this is permanent, or if there is a workaround. @unittest.skipIf(django.VERSION < (1, 7), 'Not supported by South') class SecondAppFKToTestAppMigrationTest(MigrationTest): """We don't actually migrate anything here, before and after are the same. We just check that the model we load can be linked and saved, even if they come from different apps. + See SecondAppFKToTestAppUsingPksMigrationTest for a + south-supported approach. + """ app_name = 'test_second_app' @@ -98,6 +100,28 @@ def test_save_and_reload_model(self): mymodelsecond.save() +class SecondAppFKToTestAppUsingPksMigrationTest(MigrationTest): + """Copy of SecondAppFKToTestAppMigrationTest but uses pks instead of + django's orm magic. Also have to declare in before and after the + migrations for all the models that are used. + + """ + + before = [('test_app', '0005'), ('test_second_app', '0003')] + after = [('test_app', '0005'), ('test_second_app', '0003')] + + def test_save_and_reload_model(self): + MyModelSecond = self.get_model_before('test_second_app.MyModel') + MyModelFirst = self.get_model_before('test_app.MyModel') + + mymodelfirst = MyModelFirst() + mymodelfirst.save() + + mymodelsecond = MyModelSecond() + mymodelsecond.my_model_id = mymodelfirst.pk + mymodelsecond.save() + + class MigrateFromZero(MigrationTest): before = [('test_app', '0002'), ('test_second_app', 'zero')] after = [('test_app', '0002'), ('test_second_app', '0001')]