django_prefetch_utils.selector¶
This module provides utilities for changing the implementation of
prefetch_related_objects that Django uses. In order for these to
work, enable_fetch_related_objects_selector() must be called.
This will be done in AppConfig.ready if django_prefetch_utils
is added to INSTALLED_APPS.
Once that has been called, then
set_default_prefetch_related_objects() can be called to override
the default implementation globally:
from django_prefetch_related.selector import set_default_prefetch_related_objects
from django_prefetch_utils.identity_map import prefetch_related_objects
set_default_prefetch_related_objects(prefetch_related_objects)
This will be done as part of AppConfig.ready if the
PREFETCH_UTILS_DEFAULT_IMPLEMENTATION setting is provided.
To change the implementation used on a local basis, the
override_prefetch_related_objects() or
use_original_prefetch_related_objects() context decorators can
be used:
from django_prefetch_utils.identity_map import prefetch_related_objects
@use_original_prefetch_related_objects()
def some_function():
dogs = list(Dog.objects.all()) # uses Django's implementation
with override_prefetch_related_objects(prefetch_related_objects):
toys = list(Toy.objects.all) # uses identity map implementation
Changes
django.db.models.query.prefetch_related_objectsto Django’s original implementation ofprefetch_related_objects.
Changes
django.db.models.query.prefetch_related_objectsto an implemention which allows thread-local overrides.
Returns the active implementation of
prefetch_related_objects:>>> from django_prefetch_utils.selector import get_prefetch_related_objects >>> get_prefetch_related_objects() <function django.db.models.query.prefetch_related_objects>
Returns: a function
This context decorator allows one to chnage the implementation of
prefetch_related_objectsto be func.When the context manager or decorator exits, the implementation will be restored to its previous value.
with override_prefetch_related_objects(prefetch_related_objects): dogs = list(Dog.objects.prefetch_related('toys'))
Note
This requires
enable_prefetch_related_objects_selector()to be run before the changes are able to take effect.
Removes a custom default implementation of
prefetch_related_objects:>>> set_default_prefetch_related_objects(some_implementation) >>> get_prefetch_related_objects() <function some_implementation> >>> remove_default_prefetch_related_objects() >>> get_prefetch_related_objects() <function django.db.models.query.prefetch_related_objects>
Sets the default implementation of
prefetch_related_objectsto be func:>>> get_prefetch_related_objects() <function django.db.models.query.prefetch_related_objects> >>> set_default_prefetch_related_objects(some_implementation) >>> get_prefetch_related_objects() <function some_implementation>
This context decorator allows one to force the
prefetch_related_objectsimplementation to be Django’s default implementation:with use_original_prefetch_related_objects(): dogs = list(Dog.objects.prefetch_related('toys'))