Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
like image 255
RA123 Avatar asked Aug 11 '15 21:08

RA123


People also ask

How do I register a signal in Django?

To receive a signal, register a receiver function using the Signal. connect() method. The receiver function is called when the signal is sent. All of the signal's receiver functions are called one at a time, in the order they were registered.

What is the use of the Post_delete signal in Django?

To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.

What is pre save signal in Django?

pre_save) is provoked just before the model save() method is called, or you could say model save method is called only after pre_save is called and done its job free of errors.

What is Post_save in Django?

django.db.models.signals. post_save. Like pre_save , but sent at the end of the save() method. Arguments sent with this signal: sender.


1 Answers

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.

like image 91
Alasdair Avatar answered Oct 09 '22 06:10

Alasdair