DAMAR225
DAMAR225

Reputation: 2437

Add user to group automatically after registration in Wagtail

I made this before with django like this:

signals/handlers.py

from django.dispatch import receiver
from django.conf import settings
from django.contrib.auth.models import Group
from users.models import *

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def save_profile(sender, instance, created, **kwargs):
    if created:
        g1 = Group.objects.get(name='Editors')
        instance.groups.add(g1)

apps.py

from django.apps import AppConfig


class RegistrationConfig(AppConfig):
    name = 'registration'

    def ready(self):
        import registration.signals.handlers

but I don't know how to make it with wagtail !

thanks.

Upvotes: 3

Views: 954

Answers (1)

Mon
Mon

Reputation: 86

Instead of using Django signals, Wagtail has hooks that simplify this for you. You can also send password reset email etc... after creating user using the same technique.

Just create a wagtail_hooks.py in your app:

from django.contrib.auth.models import Group
from wagtail.core import hooks
    
@hooks.register('after_create_user')
def add_user_to_group(request, user):
    if user:
       group, created = Group.objects.get_or_create(name='Group Name')
       user.groups.add(group)

Docs: https://docs.wagtail.io/en/latest/reference/hooks.html?highlight=after_create_user#id40

Upvotes: 4

Related Questions