django_prefetch_utils.identity_map

get_default_prefetch_identity_map()[source]

Returns an empty default identity map for use during prefetching.

Return type:django_prefetch_utils.identity_map.maps.PrefetchIdentityMap
get_prefetched_objects_from_list(obj_list, through_attr)[source]

Returns all of the related objects in obj_list from through_attr.

Return type:list
get_prefetcher(obj_list, through_attr, to_attr)[source]

For the attribute through_attr on the given instance, finds an object that has a get_prefetch_queryset().

Returns a 4 tuple containing:

  • (the object with get_prefetch_queryset (or None),
  • the descriptor object representing this relationship (or None),
  • a boolean that is False if the attribute was not found at all,
  • a list of the subset of obj_list that requires fetching

Calls prefetch_related_objects_impl() with a new identity map from get_default_prefetch_identity_map():

>>> from django_prefetch_utils.identity_map import prefetch_related_objects
>>> dogs = list(Dogs.objectss.all())
>>> prefetch_related_objects(dogs, 'toys')

Note

This will create will not preserve the identity map across different calls to prefetched_related_objects. For that, you need to use django_prefetch_utils.identity_map.persistent.use_persistent_prefetch_identity_map()

An implementation of prefetch_related_objects which makes use of identity_map to keep track of all of the objects which have been fetched and reuses them where possible.

use_prefetch_identity_map()[source]

A context decorator which enables the identity map version of prefetch_related_objects:

with use_prefetch_identity_map():
    dogs = list(Dogs.objects.prefetch_related('toys'))

Note

A new identity map is created and used for each call of prefetched_related_objects.

Persistent Identity Map

class FetchAllDescriptor[source]

This descriptor replaces QuerySet._fetch_all and applies an identity map to any objects fetched in a queryset.

disable_fetch_all_descriptor()[source]

Sets QuerySet._fetch_all to be the original method.

enable_fetch_all_descriptor()[source]

Replaces QuerySet._fetch_all with an instance of FetchAllDescriptor.

class use_persistent_prefetch_identity_map(identity_map=None, pass_identity_map=False)[source]

A context decorator which allows the same identity map to be used across multiple calls to prefetch_related_objects.

with use_persistent_prefetch_identity_map():
    dogs = list(Dogs.objects.prefetch_related("toys"))

    # The toy.dog instances will be identitical (not just equal)
    # to the ones fetched on the line above
    with self.assertNumQueries(1):
        toys = list(Toy.objects.prefetch_related("dog"))

Maps

class ExtraIdentityMap(extra, wrapped)[source]

This identity map wrapper

class PrefetchIdentityMap[source]

This class represents an identity map used to help ensure that equal Django model instances are identical during the prefetch process.

>>> identity_map = PrefetchIdentityMap()
>>> a = Author.objects.first()
>>> b = Author.objects.first()
>>> a is b
False
>>> identity_map[a] is a
True
>>> identity_map[b] is a
True

It is implemented as a defaultdictionary whose keys correspond the to types of Django models and whose values are a weakref.WeakValueDictionary mapping primary keys to the associated Django model instance.

get_map_for_model(model)[source]

Returns the the underlying dictionary

Return type:weakref.WeakValueDictionary
class RelObjAttrMemoizingIdentityMap(rel_obj_attr, wrapped)[source]

A wrapper for an identity map which provides a rel_obj_attr() to be returned from a get_prefetch_queryset method.

This is useful for cases when there is identifying information on the related object returned from the prefetcher which is not present on the equivalent object in the identity map.

Wrappers

class ForwardDescriptorPrefetchQuerySetWrapper(identity_map, field, instances_dict, prefix, queryset)[source]
class ForwardDescriptorPrefetchWrapper(identity_map, wrapped)[source]
class GenericForeignKeyPrefetchQuerySetWrapper(identity_map, prefix, queryset)[source]

This wrapper yields the contents of _self_prefix before yielding the contents of the wrapped object.

class GenericForeignKeyPrefetchWrapper(identity_map, wrapped)[source]
class IdentityMapIteratorWrapper(identity_map, wrapped)[source]

This is a wrapper around an iterator which applies an identity map to each of the items returned.

class IdentityMapObjectProxy(identity_map, wrapped)[source]

A generic base class for any wrapper which needs to have access to an identity map.

class IdentityMapPrefetchQuerySetWrapper(identity_map, queryset)[source]

A generic wrapper for the rel_qs queryset returned by get_prefetch_queryset methods.

Subclasses should implement the __iter__() method to customize the behavior when the queryset is iterated over.

class IdentityMapPrefetcher(identity_map, wrapped)[source]

A wrapper for any object which has a get_prefetch_queryset method.

class ManyToManyPrefetchQuerySetWrapper(identity_map, queryset, rel_obj_attr)[source]
class ManyToManyRelatedManagerWrapper(identity_map, wrapped)[source]
class ReverseManyToOneDescriptorPrefetchWrapper(identity_map, wrapped)[source]
class ReverseManyToOnePrefetchQuerySetWrapper(identity_map, field, instances_dict, queryset)[source]
class ReverseOneToOneDescriptorPrefetchWrapper(identity_map, wrapped)[source]
class ReverseOneToOnePrefetchQuerySetWrapper(identity_map, related, instances_dict, queryset)[source]