#32112: ./manage.py migrate generating invalid django_content_type inserts
-------------------------------------+-------------------------------------
     Reporter:  jamercee             |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  closed
    Component:                       |                  Version:  3.1
  contrib.contenttypes               |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by flyte):

 I had this problem on Django 3.1.7 because the `contenttypes` migration
 `0002_remove_content_type_name` was not being run before my migration.

 Fixed it by adding it as a dependency:

 {{{
 from django.apps import apps
 from django.contrib.auth.management import create_permissions
 from django.db import migrations


 def create_all_permissions(*args, **kwargs):
     for app_config in apps.get_app_configs():
         app_config.models_module = True
         create_permissions(app_config, apps=apps, verbosity=0)
         app_config.models_module = None


 def create_moderation_group(apps, schema_editor):
     Permission = apps.get_model("auth", "Permission")
     perms = Permission.objects.filter(
         codename__in=["moderate_submission", "view_submission"]
     )
     assert len(perms) == 2
     Group = apps.get_model("auth", "Group")
     group = Group.objects.create(name="Submission Moderators")
     group.permissions.set(perms)


 class Migration(migrations.Migration):

     dependencies = [
         ("api", "0001_initial"),
         ("contenttypes", "0002_remove_content_type_name"),
     ]

     operations = [
         migrations.RunPython(create_all_permissions),
         migrations.RunPython(create_moderation_group),
     ]
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32112#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.a48005094417bc5a60911e70502b8569%40djangoproject.com.

Reply via email to