1
0
mirror of https://github.com/django/django.git synced 2025-10-28 08:06:09 +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

@@ -662,6 +662,15 @@ class QuerySet(AltersData):
Create a new object with the given kwargs, saving it to the database
and returning the created object.
"""
reverse_one_to_one_fields = frozenset(kwargs).intersection(
self.model._meta._reverse_one_to_one_field_names
)
if reverse_one_to_one_fields:
raise ValueError(
"The following fields do not exist in this model: %s"
% ", ".join(reverse_one_to_one_fields)
)
obj = self.model(**kwargs)
self._for_write = True
obj.save(force_insert=True, using=self.db)