U
U
user_of_toster2020-09-09 16:46:42
Django
user_of_toster, 2020-09-09 16:46:42

Why aren't groups populated automatically after a Django migration?

The task is to automatically create groups after starting the project and add the list of permissions.

The implementation is the post_migrate signal. That is, after each migration, create groups and a list of permissions

Questions:
1) Why does this code work:

@receiver(post_migrate)
def populate_models(sender, **kwargs):
    print('Populating groups....')
....


and this one doesn't?
@receiver(post_migrate, sender=apps.BoardConfig)
def populate_models(sender, **kwargs):
    print('Populating groups....')
....


After migrate, in the first case, the code is executed 5 times, and in the code below, the code is not executed at all. Did I specify sender incorrectly?

I know about putting connect() into ready(), but I want to use @receiverand isolate signals in a separate file

2) Is signaling the best way to implement the task? Something tells me that it's wrong to run this code after every migration.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-09-09
@user_of_toster

The sender is the config object and not the config class. And the optimal implementation is highly dependent on the context.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question