# Create groups and add permissions to them in migrations

**URL:** <https://forum.djangoproject.com/t/create-groups-and-add-permissions-to-them-in-migrations/11842>\
**Category:** Using Django\
**Created:** [January 27, 2022, 6:07pm UTC](https://forum.djangoproject.com/t/create-groups-and-add-permissions-to-them-in-migrations/11842 "2022-01-27T18:07:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![supermario18b](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/supermario18b/32/6309_2.png) [@supermario18b](https://forum.djangoproject.com/u/supermario18b)\
**Post date:** [January 27, 2022, 6:07pm UTC](https://forum.djangoproject.com/t/create-groups-and-add-permissions-to-them-in-migrations/11842/1 "2022-01-27T18:07:57Z")

</div>

Hi all,

I’d like to automatically create groups and add custom permissions and some normal permission to them.

After a lot of googling I found a solution that seems ok to me, but I don’t know if it is a good practice.  
I post the link of the page, If I can’t do that, let me know and I’ll remove it:

[link](https://dandavies99.github.io/posts/2021/11/django-permissions/#goal-2-set-up-groups-when-the-project-is-initialised-using-a-data-migration)

First question:  
In that code when the migration runs, all permissions are created before django tries to do that. Is it ok?

Second question:  
If I like (for example in two months) to add a new group and assign some permission to it, I have to create a new migration and when the migration runs, all permissions are created again (it will be tried to create all the permissions again). Is it ok?

Thanks in Advance,

supermario18b

---

<div class="post-metadata">

**Author:** ![supermario18b](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/supermario18b/32/6309_2.png) [@supermario18b](https://forum.djangoproject.com/u/supermario18b)\
**Post date:** [January 27, 2022, 7:24pm UTC](https://forum.djangoproject.com/t/create-groups-and-add-permissions-to-them-in-migrations/11842/2 "2022-01-27T19:24:37Z")

</div>

the code that I’m worried about, that will be executed every time (every migration), is:

```auto
# Permissions have to be created before applying them
for app_config in apps.get_app_configs():
	app_config.models_module = True
	create_permissions(app_config, verbosity=0)
	app_config.models_module = None

```

---

<div class="post-metadata">

**Author:** ![supermario18b](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/supermario18b/32/6309_2.png) [@supermario18b](https://forum.djangoproject.com/u/supermario18b)\
**Post date:** [January 28, 2022, 3:08pm UTC](https://forum.djangoproject.com/t/create-groups-and-add-permissions-to-them-in-migrations/11842/3 "2022-01-28T15:08:56Z")

</div>

I checked the code of the “create\_permission” function and it will only be tried to add a permission if not present.

```auto
perms = [
        Permission(codename=codename, name=name, content_type=ct)
        for ct, (codename, name) in searched_perms
        if (ct.pk, codename) not in all_perms
    ]
    Permission.objects.using(using).bulk_create(perms)

```

I tested and it seems to work just fine. If someone else needs to automatically add groups and assign permission to them, this could be a way 🙂
