From 0f9da161df829bc11c8d4163e5574c2317ffe0a7 Mon Sep 17 00:00:00 2001 From: Anubhav Kushwaha Date: Thu, 13 Oct 2022 01:17:38 +0530 Subject: [PATCH 1/7] add support for select_related for inherited models --- polymorphic/query.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/polymorphic/query.py b/polymorphic/query.py index 86774898..0fd35619 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -112,6 +112,8 @@ def __init__(self, *args, **kwargs): # to that queryset as well). self.polymorphic_deferred_loading = (set(), True) + self._polymorphic_select_related = {} + def _clone(self, *args, **kwargs): # Django's _clone only copies its own variables, so we need to copy ours here new = super()._clone(*args, **kwargs) @@ -421,7 +423,12 @@ class self.model, but as a class derived from self.model. We want to re-fetch **{("%s__in" % pk_name): idlist} ) # copy select related configuration to new qs - real_objects.query.select_related = self.query.select_related + if real_concrete_class in self._polymorphic_select_related: + real_objects = real_objects.select_related( + *self._polymorphic_select_related[real_concrete_class] + ) + else: + real_objects.query.select_related = self.query.select_related # Copy deferred fields configuration to the new queryset deferred_loading_fields = [] @@ -535,3 +542,7 @@ def get_real_instances(self, base_result_objects=None): return olist clist = PolymorphicQuerySet._p_list_class(olist) return clist + + def select_polymorphic_related(self, polymorphic_subclass, *fields): + self._polymorphic_select_related[polymorphic_subclass] = fields + return self From bb5468f2a3f22d8d1e088561e1b6cc456e0af818 Mon Sep 17 00:00:00 2001 From: Anubhav Kushwaha Date: Thu, 13 Oct 2022 01:39:22 +0530 Subject: [PATCH 2/7] clone polymorphic related --- polymorphic/query.py | 1 + 1 file changed, 1 insertion(+) diff --git a/polymorphic/query.py b/polymorphic/query.py index 0fd35619..23658998 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -122,6 +122,7 @@ def _clone(self, *args, **kwargs): copy.copy(self.polymorphic_deferred_loading[0]), self.polymorphic_deferred_loading[1], ) + new._polymorphic_select_related = copy.copy(self._polymorphic_select_related) return new def as_manager(cls): From 4f9a1f5e923fb789a37950350f0b5361c625f92f Mon Sep 17 00:00:00 2001 From: Anubhav Kushwaha Date: Thu, 13 Oct 2022 19:41:25 +0530 Subject: [PATCH 3/7] added prefetch_polymorphic_related --- polymorphic/query.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/polymorphic/query.py b/polymorphic/query.py index 23658998..a538b43c 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -113,6 +113,7 @@ def __init__(self, *args, **kwargs): self.polymorphic_deferred_loading = (set(), True) self._polymorphic_select_related = {} + self._polymorphic_prefetch_related = {} def _clone(self, *args, **kwargs): # Django's _clone only copies its own variables, so we need to copy ours here @@ -123,6 +124,7 @@ def _clone(self, *args, **kwargs): self.polymorphic_deferred_loading[1], ) new._polymorphic_select_related = copy.copy(self._polymorphic_select_related) + new._polymorphic_prefetch_related = copy.copy(self._polymorphic_prefetch_related) return new def as_manager(cls): @@ -423,7 +425,8 @@ class self.model, but as a class derived from self.model. We want to re-fetch real_objects = real_concrete_class._base_objects.db_manager(self.db).filter( **{("%s__in" % pk_name): idlist} ) - # copy select related configuration to new qs + + # set select_related() on the real objects, if explicitly specified otherwise copy it from the base objects if real_concrete_class in self._polymorphic_select_related: real_objects = real_objects.select_related( *self._polymorphic_select_related[real_concrete_class] @@ -431,6 +434,12 @@ class self.model, but as a class derived from self.model. We want to re-fetch else: real_objects.query.select_related = self.query.select_related + # polymorphic prefetch related configuration to new qs + if real_concrete_class in self._polymorphic_prefetch_related: + real_objects = real_objects.prefetch_related( + *self._polymorphic_prefetch_related[real_concrete_class] + ) + # Copy deferred fields configuration to the new queryset deferred_loading_fields = [] existing_fields = self.polymorphic_deferred_loading[0] @@ -547,3 +556,7 @@ def get_real_instances(self, base_result_objects=None): def select_polymorphic_related(self, polymorphic_subclass, *fields): self._polymorphic_select_related[polymorphic_subclass] = fields return self + + def prefetch_polymorphic_related(self, polymorphic_subclass, *lookups): + self._polymorphic_prefetch_related[polymorphic_subclass] = lookups + return self From 480c30bf82d50b1829edde8f0ddbbb78d5ce517a Mon Sep 17 00:00:00 2001 From: Anubhav Kushwaha Date: Fri, 28 Oct 2022 19:16:52 +0530 Subject: [PATCH 4/7] added support for custom queryset for child objects --- polymorphic/query.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/polymorphic/query.py b/polymorphic/query.py index a538b43c..fd00754e 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -114,6 +114,7 @@ def __init__(self, *args, **kwargs): self._polymorphic_select_related = {} self._polymorphic_prefetch_related = {} + self._polymorphic_custom_queryset = {} def _clone(self, *args, **kwargs): # Django's _clone only copies its own variables, so we need to copy ours here @@ -125,6 +126,7 @@ def _clone(self, *args, **kwargs): ) new._polymorphic_select_related = copy.copy(self._polymorphic_select_related) new._polymorphic_prefetch_related = copy.copy(self._polymorphic_prefetch_related) + new._polymorphic_custom_queryset = copy.copy(self._polymorphic_custom_queryset) return new def as_manager(cls): @@ -422,7 +424,12 @@ class self.model, but as a class derived from self.model. We want to re-fetch # TODO: defer(), only(): support for these would be around here for real_concrete_class, idlist in idlist_per_model.items(): indices = indexlist_per_model[real_concrete_class] - real_objects = real_concrete_class._base_objects.db_manager(self.db).filter( + if self._polymorphic_custom_queryset[real_concrete_class]: + real_objects = self._polymorphic_custom_queryset[real_concrete_class] + else: + real_objects = real_concrete_class._base_objects.db_manager(self.db) + + real_objects = real_objects.filter( **{("%s__in" % pk_name): idlist} ) @@ -560,3 +567,6 @@ def select_polymorphic_related(self, polymorphic_subclass, *fields): def prefetch_polymorphic_related(self, polymorphic_subclass, *lookups): self._polymorphic_prefetch_related[polymorphic_subclass] = lookups return self + + def custom_queryset(self, polymorphic_subclass, queryset): + self._polymorphic_custom_queryset[polymorphic_subclass] = queryset From 82431f8083395c0e0f980a4be999bebf4324468d Mon Sep 17 00:00:00 2001 From: Anubhav Kushwaha Date: Fri, 28 Oct 2022 19:22:49 +0530 Subject: [PATCH 5/7] fix support for custom queryset for child objects --- polymorphic/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polymorphic/query.py b/polymorphic/query.py index fd00754e..c40d6658 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -424,7 +424,7 @@ class self.model, but as a class derived from self.model. We want to re-fetch # TODO: defer(), only(): support for these would be around here for real_concrete_class, idlist in idlist_per_model.items(): indices = indexlist_per_model[real_concrete_class] - if self._polymorphic_custom_queryset[real_concrete_class]: + if self._polymorphic_custom_queryset.get(real_concrete_class): real_objects = self._polymorphic_custom_queryset[real_concrete_class] else: real_objects = real_concrete_class._base_objects.db_manager(self.db) From 7304d6c16f882dea77ed3ee5924735cacb407f9f Mon Sep 17 00:00:00 2001 From: Anubhav Kushwaha Date: Fri, 28 Oct 2022 19:43:30 +0530 Subject: [PATCH 6/7] add cloning to added methods --- polymorphic/query.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/polymorphic/query.py b/polymorphic/query.py index c40d6658..80ab09de 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -561,12 +561,16 @@ def get_real_instances(self, base_result_objects=None): return clist def select_polymorphic_related(self, polymorphic_subclass, *fields): - self._polymorphic_select_related[polymorphic_subclass] = fields - return self + clone = self._clone() + clone._polymorphic_select_related[polymorphic_subclass] = fields + return clone def prefetch_polymorphic_related(self, polymorphic_subclass, *lookups): - self._polymorphic_prefetch_related[polymorphic_subclass] = lookups - return self + clone = self._clone() + clone._polymorphic_prefetch_related[polymorphic_subclass] = lookups + return clone def custom_queryset(self, polymorphic_subclass, queryset): - self._polymorphic_custom_queryset[polymorphic_subclass] = queryset + clone = self._clone() + clone._polymorphic_custom_queryset[polymorphic_subclass] = queryset + return clone From 4c08db27b7175d2499808c30d93d27ae834bdd71 Mon Sep 17 00:00:00 2001 From: Anubhav Kushwaha Date: Wed, 9 Nov 2022 18:31:41 +0530 Subject: [PATCH 7/7] fix polymorphic select_related for query if already defined on base query --- polymorphic/query.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/polymorphic/query.py b/polymorphic/query.py index 80ab09de..5eca6334 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -433,15 +433,16 @@ class self.model, but as a class derived from self.model. We want to re-fetch **{("%s__in" % pk_name): idlist} ) - # set select_related() on the real objects, if explicitly specified otherwise copy it from the base objects + # copy select_related() fields from base objects to real objects + real_objects.query.select_related = self.query.select_related + + # polymorphic select_related() fields if any if real_concrete_class in self._polymorphic_select_related: real_objects = real_objects.select_related( *self._polymorphic_select_related[real_concrete_class] ) - else: - real_objects.query.select_related = self.query.select_related - # polymorphic prefetch related configuration to new qs + # polymorphic prefetch related configuration to new qs if real_concrete_class in self._polymorphic_prefetch_related: real_objects = real_objects.prefetch_related( *self._polymorphic_prefetch_related[real_concrete_class] @@ -561,6 +562,10 @@ def get_real_instances(self, base_result_objects=None): return clist def select_polymorphic_related(self, polymorphic_subclass, *fields): + if self.query.select_related is True: + raise ValueError( + "select_polymorphic_related() cannot be used together with select_related=True" + ) clone = self._clone() clone._polymorphic_select_related[polymorphic_subclass] = fields return clone