mirror of
https://github.com/django/django.git
synced 2025-08-21 01:09:13 +00:00
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.
This commit is contained in:
parent
cd0966cd4e
commit
8b229b4dbb
@ -89,20 +89,6 @@ def inject_rename_contenttypes_operations(
|
|||||||
migration.operations.insert(inserted + index, operation)
|
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(
|
def create_contenttypes(
|
||||||
app_config,
|
app_config,
|
||||||
verbosity=2,
|
verbosity=2,
|
||||||
@ -117,29 +103,33 @@ def create_contenttypes(
|
|||||||
if not app_config.models_module:
|
if not app_config.models_module:
|
||||||
return
|
return
|
||||||
|
|
||||||
app_label = app_config.label
|
|
||||||
try:
|
try:
|
||||||
app_config = apps.get_app_config(app_label)
|
app_config = apps.get_app_config(app_config.label)
|
||||||
ContentType = apps.get_model("contenttypes", "ContentType")
|
ContentType = apps.get_model("contenttypes", "ContentType")
|
||||||
except LookupError:
|
except LookupError:
|
||||||
return
|
return
|
||||||
|
|
||||||
content_types, app_models = get_contenttypes_and_models(
|
if not router.allow_migrate_model(using, ContentType):
|
||||||
app_config, using, ContentType
|
|
||||||
)
|
|
||||||
|
|
||||||
if not app_models:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
cts = [
|
all_model_names = {model._meta.model_name for model in app_config.get_models()}
|
||||||
ContentType(
|
|
||||||
app_label=app_label,
|
if not all_model_names:
|
||||||
model=model_name,
|
return
|
||||||
|
|
||||||
|
ContentType.objects.clear_cache()
|
||||||
|
|
||||||
|
existing_model_names = set(
|
||||||
|
ContentType.objects.using(using)
|
||||||
|
.filter(app_label=app_config.label)
|
||||||
|
.values_list("model", flat=True)
|
||||||
)
|
)
|
||||||
for (model_name, model) in app_models.items()
|
|
||||||
if model_name not in content_types
|
cts = [
|
||||||
|
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)
|
ContentType.objects.using(using).bulk_create(cts)
|
||||||
if verbosity >= 2:
|
if verbosity >= 2:
|
||||||
for ct in cts:
|
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}'")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user