summaryrefslogtreecommitdiff
path: root/django/contrib/contenttypes
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2025-04-23 11:28:59 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2025-08-16 20:48:53 +0200
commit8b229b4dbb6db08428348aeb7e5a536b64cf8ed8 (patch)
tree3b6aff20ac2fe67632cae9efd82291203b12359d /django/contrib/contenttypes
parentcd0966cd4e37da8e6153cbf57c194dce29caaddc (diff)
Refs #31123 -- Simplified create_contentypes().
Since 142ab6846ac09d6d401e26fc8b6b988a583ac0f5 get_contenttypes_and_models() function was only used in this module and we only needed the model names, not the content type objects themselves.
Diffstat (limited to 'django/contrib/contenttypes')
-rw-r--r--django/contrib/contenttypes/management/__init__.py44
1 files changed, 17 insertions, 27 deletions
diff --git a/django/contrib/contenttypes/management/__init__.py b/django/contrib/contenttypes/management/__init__.py
index 903b9ab1a0..929e44f390 100644
--- a/django/contrib/contenttypes/management/__init__.py
+++ b/django/contrib/contenttypes/management/__init__.py
@@ -89,20 +89,6 @@ def inject_rename_contenttypes_operations(
migration.operations.insert(inserted + index, operation)
-def get_contenttypes_and_models(app_config, using, ContentType):
- if not router.allow_migrate_model(using, ContentType):
- return None, None
-
- ContentType.objects.clear_cache()
-
- content_types = {
- ct.model: ct
- for ct in ContentType.objects.using(using).filter(app_label=app_config.label)
- }
- app_models = {model._meta.model_name: model for model in app_config.get_models()}
- return content_types, app_models
-
-
def create_contenttypes(
app_config,
verbosity=2,
@@ -117,29 +103,33 @@ def create_contenttypes(
if not app_config.models_module:
return
- app_label = app_config.label
try:
- app_config = apps.get_app_config(app_label)
+ app_config = apps.get_app_config(app_config.label)
ContentType = apps.get_model("contenttypes", "ContentType")
except LookupError:
return
- content_types, app_models = get_contenttypes_and_models(
- app_config, using, ContentType
- )
+ if not router.allow_migrate_model(using, ContentType):
+ return
- if not app_models:
+ all_model_names = {model._meta.model_name for model in app_config.get_models()}
+
+ if not all_model_names:
return
+ ContentType.objects.clear_cache()
+
+ existing_model_names = set(
+ ContentType.objects.using(using)
+ .filter(app_label=app_config.label)
+ .values_list("model", flat=True)
+ )
+
cts = [
- ContentType(
- app_label=app_label,
- model=model_name,
- )
- for (model_name, model) in app_models.items()
- if model_name not in content_types
+ ContentType(app_label=app_config.label, model=model_name)
+ for model_name in sorted(all_model_names - existing_model_names)
]
ContentType.objects.using(using).bulk_create(cts)
if verbosity >= 2:
for ct in cts:
- print("Adding content type '%s | %s'" % (ct.app_label, ct.model))
+ print(f"Adding content type '{ct.app_label} | {ct.model}'")