Skip to main content

GenericPrefetch

This class extends the standard prefetch functionality to support multiple querysets for a single lookup. It validates that provided querysets are compatible with prefetching, specifically excluding raw queries or those returning non-model results. The class also manages the internal state of these querysets to ensure they are not evaluated prematurely during serialization.

Attributes

AttributeTypeDescription
querysetslistA list of QuerySet objects used to fetch related data for different content types in a generic relation.

Constructor

Signature

def GenericPrefetch(
lookup: string,
querysets: list,
to_attr: string = None
)

Parameters

NameTypeDescription
lookupstringThe relation to prefetch.
querysetslistA list of querysets to use for the prefetch operation.
to_attrstring = NoneAn optional attribute to store the result in.

Signature

def GenericPrefetch(
lookup: string,
querysets: list,
to_attr: string = None
) - > null

Parameters

NameTypeDescription
lookupstringThe relation name or path to traverse for prefetching.
querysetslistA collection of QuerySet instances used to fetch the related objects; must not use raw(), values(), or values_list().
to_attrstring = NoneAn optional attribute name to store the prefetched results on the parent objects.

Methods


get_current_querysets()

@classmethod
def get_current_querysets(
level: integer
) - > list|null

Returns the list of querysets associated with this prefetch operation if the current traversal level matches the target prefetch path.

Parameters

NameTypeDescription
levelintegerThe current depth or path level in the prefetch traversal process.

Returns

TypeDescription
`listnull`