perf(filters): Avoid ContentType join in ContentTypeFilter

Resolve the ContentType via get_by_natural_key() and filter by the
FK value to prevent an unnecessary join to django_content_type.

Fixes #21420
This commit is contained in:
Martin Hauser
2026-02-16 17:29:26 +01:00
committed by Jeremy Stretch
parent 862593f2dd
commit f4c3c90bab

View File

@@ -165,12 +165,12 @@ class ContentTypeFilter(django_filters.CharFilter):
try: try:
app_label, model = value.lower().split('.') app_label, model = value.lower().split('.')
except ValueError: content_type = ContentType.objects.get_by_natural_key(app_label, model)
except (ValueError, ContentType.DoesNotExist):
return qs.none() return qs.none()
return qs.filter( return qs.filter(
**{ **{
f'{self.field_name}__app_label': app_label, f'{self.field_name}': content_type,
f'{self.field_name}__model': model
} }
) )