I have a Django project called expenses, and an app called expenseitems. In the project settings INSTALLED_APPS contains expenses.expenseitems
. When I invoke the migrate command, the database is set up and the tables are created as expected.
My end goal is to create a group-level permission for my model. So I want to invoke a method after migration is complete. However, while the ready methods in the AppConfigs for django.contrib.auth
or django.contrib.admin
are invoked when running migrate, the ready method for my app is not called.
Here is my apps.py:
from django.apps import AppConfig
from django.db.models.signals import post_migrate
def initialization(sender, **kwargs):
from django.contrib.auth.models import Group, Permission
public, created = Group.objects.get_or_create(name="public")
if not public.permissions.filter(codename="change_expenseitems").exists():
perm = Permission.objects.get(codename="change_expenseitems")
public.permissions.add(perm)
class ExpenseitemConfig(AppConfig):
name = 'expenses.expenseitems'
def ready(self):
post_migrate.connect(initialization, sender=self)
Any suggestions on what I'm missing?