Django How to send signals from User model for add or remove members from group?

I am trying to use signals for add and remove members from groups but I am not understanding where I am doing wrong. here is my code:

@receiver(post_save,sender=settings.AUTH_USER_MODEL)
def group(sender,instance,created,**kwargs):
        group = Group.objects.get(name='myauthors')
        if instance.is_blog_author == True:
           instance.groups.add(group)
        elif instance.is_blog_author == False:
             instance.groups.remove(group) 

I am using Abstract user model so I am using sender=settings.AUTH_USER_MODEL instead of sender=User.

You have to save your instance and I think you don’t need to remove from the group, just like this:

@receiver(post_save,sender=settings.AUTH_USER_MODEL)
def add_to_myauthors_group(sender,instance,created,**kwargs):
        group = Group.objects.get(name='myauthors')
        if instance.is_blog_author:
           instance.groups.add(group)
           instance.save()

Not working… It’s not adding members to groups

Of course not, in that case you are making a queryset, not saving an instance.

I need to be save. how to da that?

Can I see your models?

here is my model:

class UserManagement(AbstractUser):

      is_blog_author = models.BooleanField(default=False)

      is_editor = models.BooleanField(default=False)

      is_subscriber = models.BooleanField(default=False)

      is_customer = models.BooleanField(default=False)

Until this point I think my first answer is correct, with this Signal:

@receiver(post_save,sender=settings.AUTH_USER_MODEL)
def add_to_myauthors_group(sender,instance,created,**kwargs):
        group = Group.objects.get(name='myauthors')
        if instance.is_blog_author:
           instance.groups.add(group)
           instance.save()

The instance of the User should be added t ‘myauthors’ Group.
Now, you should be careful of:

  • Does ‘myauthors’ Group exists?
  • Did you register your Signal in your app.py file?
  • Did you register your UserManagement model as the Authentication model in the in your configuration file?
1 Like
  • Does ‘myauthors’ Group exists? Yes
  • Did you register your Signal in your app.py file? No
  • Did you register your UserManagement model as the Authentication model in the in your configuration file?No.

How to register Usermanagement model as the Authentication model in my configuration file? Can you please little bit explain this topic. I also don’t know how to register signal in app.py

Read the following tutorials:

https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html

The main point here is that you have to register you Signal in the app.py file in the same app directory of your signals.py file:

from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _

class ProfilesConfig(AppConfig):
    name = 'cmdbox.profiles'
    verbose_name = _('profiles')

    def ready(self):
        import cmdbox.profiles.signals  # noqa

https://learndjango.com/tutorials/django-custom-user-model

And here the point is that you have to register your custom User model in your settings.py file:


AUTH_USER_MODEL = 'accounts.CustomUser'

I don’t want to sound pedantic, but before code a full project you should take a full Django Tutorial and /or read a good Django book for Beginners (indeed the Django for beginners book written by William Vincent is very good)

1 Like