from django.apps import AppConfig from django.db.models.signals import post_migrate def setup_groups(sender, apps, **kwargs): """ Add the appropriate permissions to the "member" and "buro" groups after the `post_migrate` signal since the permissions will only be inserted in the database at the very end of the migrations. """ Group = apps.get_model("auth", "Group") Permission = apps.get_model("auth", "Permission") # Buro members have perms bds.* and gestion.* buro, _ = Group.objects.get_or_create(name="bds_buro") app_perms = Permission.objects.filter( content_type__app_label__in=["cof", "gestion"] ) buro.permissions.add(*app_perms) # Members have perm bds.member members, _ = Group.objects.get_or_create(name="bds_members") perm = Permission.objects.get( codename="member", content_type__app_label="bds" ) members.permissions.add(perm) class BDSConfig(AppConfig): name = "bds" verbose_name = "Application de gestion du BDS" def ready(self): # https://docs.djangoproject.com/en/1.11/ref/signals/#post-migrate post_migrate.connect(setup_groups, sender=self)