0

So I want to assign a user on signup to a specific group. I am using Django Allauth. I found some posts on SO but I can't seem to figure it out.

This is my code:

from django.contrib.auth.models import Group

class CustomSignupForm(SignupForm):

    first_name = forms.CharField(max_length=30, widget=forms.TextInput(
        attrs={'type': 'text',
               'placeholder': _('First name')}))
    last_name = forms.CharField(max_length=30, widget=forms.TextInput(
        attrs={'type': 'text',
               'placeholder': _('Last name')}))
    birth_date = forms.DateField(widget=forms.DateInput(
        attrs={'type': 'date',
               'placeholder': _('Date of birth')}))
    privacy = forms.BooleanField(widget=forms.CheckboxInput())

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.birth_date = self.cleaned_data['birth_date']
        user.privacy = self.cleaned_data['privacy']

        role = request.session.get('user_type')
        group = role or "Default"
        g = Group.objects.get(name='Premium')
        user.groups.add(g)
        user.save()
        return user

However this is not working, the user does not get assigned to the group "Premium" after signup.

Any help is appreciated.

Raf Rasenberg
  • 534
  • 2
  • 14
  • 27

1 Answers1

0

Raf Rasenberg, try to check this thread: "django allauth custom signup form to assign different groups". I was able to assign the user to a specific group based on the chosen field upon signup. I'm using custom user profile based on AbstractUser class and LocalSignupForm(forms.Form) with ACCOUNT_SIGNUP_FORM_CLASS = "studios.forms.LocalSignupForm" in settings.py

Eugene S.
  • 51
  • 4