Skip to content

Commit

Permalink
Separate method for _build_prefetch to allow overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
fjsj committed May 31, 2023
1 parent bb7a526 commit fce78df
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions django_virtual_models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ def _hydrate_queryset_with_nested_declared_fields(

return new_qs

def _build_prefetch(self, prefetch_queryset: QuerySet):
if self.lookup:
return Prefetch(self.lookup, queryset=prefetch_queryset, to_attr=self.to_attr)
else:
return Prefetch(self.field_name, queryset=prefetch_queryset)

def hydrate_queryset(
self,
qs: QuerySet,
Expand Down Expand Up @@ -325,12 +331,8 @@ def hydrate_queryset(
deferred_fields=self.deferred_fields,
)

# build prefetch with lookups
if self.lookup:
prefetch = Prefetch(self.lookup, queryset=prefetch_queryset, to_attr=self.to_attr)
else:
prefetch = Prefetch(self.field_name, queryset=prefetch_queryset)

# build prefetch object, call prefetch_related
prefetch = self._build_prefetch(prefetch_queryset)
new_qs = qs.prefetch_related(prefetch)
return new_qs

Expand Down

0 comments on commit fce78df

Please sign in to comment.