Skip to content

Instantly share code, notes, and snippets.

@BertrandBordage
Forked from justinfx/cache_generics.py
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BertrandBordage/b862d8b6066ad2540c61 to your computer and use it in GitHub Desktop.
Save BertrandBordage/b862d8b6066ad2540c61 to your computer and use it in GitHub Desktop.
'''
Cache the generic relation field of all the objects
in the queryset, using larger bulk queries ahead of time.
Improved from original by Daniel Roseman:
http://blog.roseman.org.uk/2010/02/22/django-patterns-part-4-forwards-generic-relations/
'''
def cache_generics(queryset):
generics = {}
for item in queryset:
if item.object_id is not None:
generics.setdefault(item.content_type_id, set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(generics.keys())
relations = {}
for ct, fk_list in generics.iteritems():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.in_bulk(list(fk_list))
for item in queryset:
try:
cached_val = relations[item.content_type_id][item.object_id]
except KeyError:
cached_val = None
setattr(item, '_content_object_cache', cached_val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment