Don't populate ObjectTypes during migration

This commit is contained in:
Jeremy Stretch 2025-07-24 08:41:47 -04:00
parent 533fe555ea
commit 13c3ce39ee
3 changed files with 2 additions and 28 deletions

View File

@ -3,23 +3,6 @@ import django.db.models.deletion
from django.db import migrations, models
def populate_object_types(apps, schema_editor):
"""
Create an ObjectType record for each valid ContentType.
"""
ContentType = apps.get_model('contenttypes', 'ContentType')
ObjectType = apps.get_model('core', 'ObjectType')
for ct in ContentType.objects.all():
try:
# Validate ContentType
apps.get_model(ct.app_label, ct.model)
except LookupError:
continue
# TODO assign public/features
ObjectType(pk=ct.pk, features=[]).save_base(raw=True)
class Migration(migrations.Migration):
dependencies = [
@ -69,9 +52,4 @@ class Migration(migrations.Migration):
bases=('contenttypes.contenttype',),
managers=[],
),
# Create an ObjectType record for each ContentType
migrations.RunPython(
code=populate_object_types,
reverse_code=migrations.RunPython.noop,
),
]

View File

@ -22,8 +22,6 @@ class ObjectTypeQuerySet(models.QuerySet):
if (app_label := kwargs.get('app_label')) and (model := kwargs.get('model')):
try:
kwargs['contenttype_ptr'] = ContentType.objects.get(app_label=app_label, model=model)
kwargs.pop('app_label')
kwargs.pop('model')
except ObjectDoesNotExist:
pass
return super().create(**kwargs)

View File

@ -62,16 +62,14 @@ def update_object_types(sender, **kwargs):
ot = ObjectType.objects.get_by_natural_key(app_label=app_label, model=model_name)
ot.public = is_public
ot.features = features
ot.save()
except ObjectDoesNotExist:
ct = ContentType.objects.get_for_model(model)
ot = ObjectType(
contenttype_ptr=ct,
ObjectType.objects.create(
app_label=app_label,
model=model_name,
public=is_public,
features=features,
)
ot.save()
@receiver(post_save, sender=ContentType)