diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index ed98ecb48cb..62284e980e3 100644 --- a/django/contrib/contenttypes/fields.py +++ b/django/contrib/contenttypes/fields.py @@ -1,3 +1,4 @@ +import functools from collections import defaultdict from django.contrib.contenttypes.models import ContentType @@ -515,17 +516,18 @@ def create_generic_related_manager(superclass, rel): self.instance = instance self.model = rel.model - - content_type = ContentType.objects.db_manager(instance._state.db).get_for_model( - instance, for_concrete_model=rel.field.for_concrete_model) - self.content_type = content_type + self.get_content_type = functools.partial( + ContentType.objects.db_manager(instance._state.db).get_for_model, + for_concrete_model=rel.field.for_concrete_model, + ) + self.content_type = self.get_content_type(instance) self.content_type_field_name = rel.field.content_type_field_name self.object_id_field_name = rel.field.object_id_field_name self.prefetch_cache_name = rel.field.attname self.pk_val = instance.pk self.core_filters = { - '%s__pk' % self.content_type_field_name: content_type.id, + '%s__pk' % self.content_type_field_name: self.content_type.id, self.object_id_field_name: self.pk_val, }