I know I can create user groups and assign permission to them from the admin area in a django project. I can also create a group and assign permission to it by importing
python manage.py migrate
You can define a
post_migrate signal to create required
Group model instances if they don't exist already.
When you create an application in using
python manage.py startapp <app_name>, it creates an
AppConfig class in apps.py file.
You can specify which signal to call in
AppConfig class definition. Say the signal is called
populate_models. In that case, modify AppConfig to look like following:
class AppConfig(AppConfig): name = 'app' def ready(self): from .signals import populate_models post_migrate.connect(populate_models, sender=self)
And in signals.py define the
def populate_models(sender, **kwargs): from django.contrib.auth.models import User from django.contrib.auth.models import group # create groups # assign permissions to groups # create users