1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

[4.1.x] Fixed #33952 -- Reallowed creating reverse foreign key managers on unsaved instances.

Thanks Claude Paroz for the report.

Regression in 7ba6ebe914.

Backport of 806e9e2d0d from main
This commit is contained in:
David Wobrock
2022-08-24 21:14:32 +02:00
committed by Mariusz Felisiak
parent 0890719402
commit fca055315e
3 changed files with 14 additions and 9 deletions

View File

@@ -627,15 +627,6 @@ def create_reverse_many_to_one_manager(superclass, rel):
self.core_filters = {self.field.name: instance}
# Even if this relation is not to pk, we require still pk value.
# The wish is that the instance has been already saved to DB,
# although having a pk value isn't a guarantee of that.
if self.instance.pk is None:
raise ValueError(
f"{instance.__class__.__name__!r} instance needs to have a primary "
f"key value before this relationship can be used."
)
def __call__(self, *, manager):
manager = getattr(self.model, manager)
manager_class = create_reverse_many_to_one_manager(manager.__class__, rel)
@@ -700,6 +691,14 @@ def create_reverse_many_to_one_manager(superclass, rel):
pass # nothing to clear from cache
def get_queryset(self):
# Even if this relation is not to pk, we require still pk value.
# The wish is that the instance has been already saved to DB,
# although having a pk value isn't a guarantee of that.
if self.instance.pk is None:
raise ValueError(
f"{self.instance.__class__.__name__!r} instance needs to have a "
f"primary key value before this relationship can be used."
)
try:
return self.instance._prefetched_objects_cache[
self.field.remote_field.get_cache_name()