In django 1.8, how to set the sender for the receiver of the post_migrate and post_syncdb signal when the user model of the user is installed?

Below is my code in the signal.py file located 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 installed as

 sender=settings.AUTH_USER_MODEL 

so far this only works for post_save, as indicated in the sample documentation.

I have already tried get_user_model () as well as directly using my_custom_user.models .

get_user_model () returns an error, while setting models as sender works 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 the documentation, this is the wrong way to reference a user model and just an ugly workaround.

It would help me to help me with the solution, so I can add these groups with the first migration of the user model.

thanks

EDIT: using get_user_model () returns the following error -

 django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. 
+5
source share
1 answer

sender for the post_migrate method post_migrate never a model (in any other way), it is an instance of AppConfig for the application that was installed.

The docs give an example of 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 the post_sync_db signal (note that the signal is out of date) is a module that contains the installed models.

+7
source

All Articles