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.