in django 1.8, how to set sender for post_migrate and post_syncdb signal receiver when a custom user model is set?
Asked Answered
D

1

5

Following is my code in the signals.py file placed in the package where the auth model is defined.

@receiver(post_migrate, sender=settings.AUTH_USER_MODEL)
def define_groups(sender, **kwargs):
    # Create groups
    Group.objects.get_or_create(name='Promoter')
    Group.objects.get_or_create(name='Client')
    Group.objects.get_or_create(name='Superuser')
    Group.objects.get_or_create(name='Staff')

The documentation (https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#referencing-the-user-model) states that it should be set as

sender=settings.AUTH_USER_MODEL

while this only works for post_save as mentioned in the documentation example.

I've already tried get_user_model() and also directly using the my_custom_user.models.

get_user_model() returns an error, while setting models as sender works just fine, as -

from . import models

@receiver(post_syncdb, sender=models)
def define_groups(sender, **kwargs):
    # Create groups
    Group.objects.get_or_create(name='Promoter')
    Group.objects.get_or_create(name='Client')
    Group.objects.get_or_create(name='Superuser')
    Group.objects.get_or_create(name='Staff')

But according to documentation this is not the right way to refer a custom user model and is just an ugly workaround.

Would someone please be able to help me with a solution so i can add these Groups with the first migration of user model.

Thank You

EDIT : using get_user_model() returns the following error -

django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
Delighted answered 11/8, 2015 at 21:39 Comment(1)
Hi @RA123, I'm trying to archive the this same issue, but my custom signal doesn't get fired. Any suggestion is welcome. Thanks in advance.Lamere
A
7

The sender for the post_migrate method is never a model (custom or otherwise), it is the AppConfig instance for the app which was installed.

The docs give the following example for connecting your signal handler in the ready method.

from django.apps import AppConfig
from django.db.models.signals import post_migrate

def my_callback(sender, **kwargs):
    # Your specific logic here
    pass

class MyAppConfig(AppConfig):
    ...

    def ready(self):
        post_migrate.connect(my_callback, sender=self)

Similarly, the sender for post_sync_db signal (note the signal is deprecated) is the module containing the models which were installed.

Aruabea answered 11/8, 2015 at 23:37 Comment(6)
Sir, Thank you for your answer, it worked perfectly and forgive me for my ignorance as i believed i could use the post_migrate like the other signals.Delighted
Hi @Alasdair, I'm trying to archive the this same issue, but my custom signal doesn't get fired. Any suggestion is welcome. Thanks in advance.Lamere
@sgmart please ask a new question rather than adding a comment to an old question.Aruabea
But I'm sure, my question is gonna be marked as duplicated.Lamere
@sgmart I doubt you are trying to use post_syncdb with a model, so it's not a duplicate of this question.Aruabea
Here's my question: #32447288, thanks in advance.Lamere

© 2022 - 2024 — McMap. All rights reserved.