From 6b7bd079a6fc4e84553262b65074dbe0af456b03 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Mon, 23 Sep 2019 15:51:43 -0400 Subject: [3.0.x] Fixed #30796 -- Prevented select_related() from mutating a queryset on chaining. Thanks Darren Maki for the report. Backport of 37f8f293775d0b672da8ae369d9a4e17f1db7851 from master --- django/db/models/sql/query.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'django/db/models/sql') diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 8e55bd092d..ea51912e8b 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -6,6 +6,7 @@ themselves do not have to (and could be backed by things other than SQL databases). The abstraction barrier only works one way: this module has to know all about the internals of models in order to get the information it needs. """ +import copy import difflib import functools import inspect @@ -324,6 +325,10 @@ class Query(BaseExpression): obj._extra_select_cache = None else: obj._extra_select_cache = self._extra_select_cache.copy() + if self.select_related is not False: + # Use deepcopy because select_related stores fields in nested + # dicts. + obj.select_related = copy.deepcopy(obj.select_related) if 'subq_aliases' in self.__dict__: obj.subq_aliases = self.subq_aliases.copy() obj.used_aliases = self.used_aliases.copy() -- cgit v1.3