1

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?

Sushil
  • 371
  • 1
  • 3
  • 11
  • 1
    @souldeux That was it! Now what, do I answer my own question? – Sushil Aug 28 '18 at 22:22
  • glad it helped! No need to any further action. Since the answer in the other question was able to help you as it was written, this question will likely be closed soon as a duplicate of that question. – souldeux Aug 28 '18 at 22:29
  • @Sushil You can also close your own question without waiting for the community to do it. – Selcuk Aug 29 '18 at 00:33

0 Answers0