Refs #30493 -- Added GenericRelatedObjectManager.get_content_type() hook.

This commit is contained in:
Mariusz Felisiak 2019-05-30 11:24:06 +02:00
parent ea6e684f34
commit f66021f3f7
1 changed files with 7 additions and 5 deletions

View File

@ -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,
}