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
| Attribute | Type | Description |
|---|---|---|
| querysets | list | A 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
| Name | Type | Description |
|---|---|---|
| lookup | string | The relation to prefetch. |
| querysets | list | A list of querysets to use for the prefetch operation. |
| to_attr | string = None | An optional attribute to store the result in. |
Signature
def GenericPrefetch(
lookup: string,
querysets: list,
to_attr: string = None
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| lookup | string | The relation name or path to traverse for prefetching. |
| querysets | list | A collection of QuerySet instances used to fetch the related objects; must not use raw(), values(), or values_list(). |
| to_attr | string = None | An 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
| Name | Type | Description |
|---|---|---|
| level | integer | The current depth or path level in the prefetch traversal process. |
Returns
| Type | Description |
|---|---|
| `list | null` |