Skip to content

Commit

Permalink
add select_related and partial prefetch_related support
Browse files Browse the repository at this point in the history
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: #198 #436 #359 #244
possible fixes:
    #498: support for prefetch_related cannot fetch attributes not on all child models or via class names
related: #531
  • Loading branch information
pgammans committed Jun 26, 2023
1 parent bd5faf0 commit 7ab11a3
Show file tree
Hide file tree
Showing 5 changed files with 1,398 additions and 54 deletions.
13 changes: 11 additions & 2 deletions polymorphic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,18 @@ def __init__(self, *args, **kwargs):
self.__class__.polymorphic_super_sub_accessors_replaced = True

def create_accessor_function_for_model(model, accessor_name):
NOT_PROVIDED = object()

def accessor_function(self):
objects = getattr(model, "_base_objects", model.objects)
attr = objects.get(pk=self.pk)
attr = NOT_PROVIDED
try:
attr = self._state.fields_cache[accessor_name]
pass
except KeyError:
pass
if attr is NOT_PROVIDED:
objects = getattr(model, "_base_objects", model.objects)
attr = objects.get(pk=self.pk)
return attr

return accessor_function
Expand Down
Loading

0 comments on commit 7ab11a3

Please sign in to comment.