diff options
author | Rob <tienrobertnguyenn@gmail.com> | 2019-05-20 22:06:30 +1000 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-05-21 10:11:22 +0200 |
commit | 519016e5f25d7c0a040015724f9920581551cab0 (patch) | |
tree | df36567aa654920242b4cf82515f810aef1532f4 /tests/many_to_one | |
parent | 266e7e0eccf1d109e8c64c7bc5e612a538ad618a (diff) | |
download | django-519016e5f25d7c0a040015724f9920581551cab0.tar.gz |
Fixed #28147 -- Fixed loss of assigned parent when saving child after parent.
Thanks Erwin Junge for the initial patch.
Diffstat (limited to 'tests/many_to_one')
-rw-r--r-- | tests/many_to_one/models.py | 4 | ||||
-rw-r--r-- | tests/many_to_one/tests.py | 12 |
2 files changed, 14 insertions, 2 deletions
diff --git a/tests/many_to_one/models.py b/tests/many_to_one/models.py index 96b84ccddb..6230129bbd 100644 --- a/tests/many_to_one/models.py +++ b/tests/many_to_one/models.py @@ -70,6 +70,10 @@ class Child(models.Model): parent = models.ForeignKey(Parent, models.CASCADE) +class ChildNullableParent(models.Model): + parent = models.ForeignKey(Parent, models.CASCADE, null=True) + + class ToFieldChild(models.Model): parent = models.ForeignKey(Parent, models.CASCADE, to_field='name', related_name='to_field_children') diff --git a/tests/many_to_one/tests.py b/tests/many_to_one/tests.py index 8ad467b6b6..7538f238f2 100644 --- a/tests/many_to_one/tests.py +++ b/tests/many_to_one/tests.py @@ -8,8 +8,8 @@ from django.test import TestCase from django.utils.translation import gettext_lazy from .models import ( - Article, Category, Child, City, District, First, Parent, Record, Relation, - Reporter, School, Student, Third, ToFieldChild, + Article, Category, Child, ChildNullableParent, City, District, First, + Parent, Record, Relation, Reporter, School, Student, Third, ToFieldChild, ) @@ -522,6 +522,14 @@ class ManyToOneTests(TestCase): self.assertIsNot(c.parent, p) self.assertEqual(c.parent, p) + def test_save_nullable_fk_after_parent(self): + parent = Parent() + child = ChildNullableParent(parent=parent) + parent.save() + child.save() + child.refresh_from_db() + self.assertEqual(child.parent, parent) + def test_save_nullable_fk_after_parent_with_to_field(self): parent = Parent(name='jeff') child = ToFieldChild(parent=parent) |