Skip to content

Commit f87be18

Browse files
pgammansandriilahuta
authored andcommitted
add select_related and partial prefetch_related support
implement support for a single query for select related base fetches across polymorphic models. adds a polymorphic QuerySet Mixin to enable non polymorphic models to fetch related models. fixes: jazzband#198 jazzband#436 jazzband#359 jazzband#244 possible fixes: jazzband#498: support for prefetch_related cannot fetch attributes not on all child models or via class names related: jazzband#531
1 parent 9d674e4 commit f87be18

File tree

5 files changed

+1396
-49
lines changed

5 files changed

+1396
-49
lines changed

polymorphic/models.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,18 @@ def __init__(self, *args, **kwargs):
198198
self.__class__.polymorphic_super_sub_accessors_replaced = True
199199

200200
def create_accessor_function_for_model(model, accessor_name):
201+
NOT_PROVIDED = object()
202+
201203
def accessor_function(self):
202-
objects = getattr(model, "_base_objects", model.objects)
203-
attr = objects.get(pk=self.pk)
204+
attr = NOT_PROVIDED
205+
try:
206+
attr = self._state.fields_cache[accessor_name]
207+
pass
208+
except KeyError:
209+
pass
210+
if attr is NOT_PROVIDED:
211+
objects = getattr(model, "_base_objects", model.objects)
212+
attr = objects.get(pk=self.pk)
204213
return attr
205214

206215
return accessor_function

0 commit comments

Comments
 (0)