1
0
mirror of https://github.com/django/django.git synced 2025-10-26 07:06:08 +00:00

Fixed #34586 -- Made QuerySet.create() raise ValueError for reverse one-to-many relations.

This commit is contained in:
Mariana
2023-07-26 23:58:58 +01:00
committed by Mariusz Felisiak
parent aa3cb3f372
commit e02fc58889
3 changed files with 43 additions and 0 deletions

View File

@@ -524,6 +524,29 @@ class OneToOneTests(TestCase):
Director._meta.base_manager_name = None
Director._meta._expire_cache()
def test_create_reverse_o2o_error(self):
msg = "The following fields do not exist in this model: restaurant"
with self.assertRaisesMessage(ValueError, msg):
Place.objects.create(restaurant=self.r1)
def test_get_or_create_reverse_o2o_error(self):
msg = "The following fields do not exist in this model: restaurant"
r2 = Restaurant.objects.create(
place=self.p2, serves_hot_dogs=True, serves_pizza=False
)
with self.assertRaisesMessage(ValueError, msg):
Place.objects.get_or_create(name="nonexistent", defaults={"restaurant": r2})
def test_update_or_create_reverse_o2o_error(self):
msg = "The following fields do not exist in this model: restaurant"
r2 = Restaurant.objects.create(
place=self.p2, serves_hot_dogs=True, serves_pizza=False
)
with self.assertRaisesMessage(ValueError, msg):
Place.objects.update_or_create(
name="nonexistent", defaults={"restaurant": r2}
)
def test_hasattr_related_object(self):
# The exception raised on attribute access when a related object
# doesn't exist should be an instance of a subclass of `AttributeError`