How do I save the selected role?

I’m using django-groups-manager.

I want to select the role when adding members. I’m listing roles, but I don’t know how to save them. How can I do it?

class CalisanForm(UserCreationForm):
username = forms.CharField(max_length=100, label='Kullanıcı Adı')
email = forms.EmailField(max_length=200, help_text='Required')
password1 = forms.CharField(max_length=100, label='Parola', widget=forms.PasswordInput)
password2 = forms.CharField(max_length=100, label='Parola Doğrulama', widget=forms.PasswordInput)
group = forms.ModelChoiceField(queryset=Group.objects.all(), widget=forms.widgets.RadioSelect(), empty_label=None)
roles = forms.ModelChoiceField(queryset=GroupMemberRole.objects.all(), widget=forms.widgets.RadioSelect(), empty_label=None)

def __init__(self, *args, **kwargs):
    qs = kwargs.pop("group_qs")
    super().__init__(*args, **kwargs)
    self.fields["group"].queryset = qs

class Meta:
    model = User
    fields = [

def clean_password2(self):
    password1 = self.cleaned_data.get('password1')
    password2 = self.cleaned_data.get('password2')
    if password1 and password2 and password1 != password2:
        raise forms.ValidationError("Parolalar eşleşmiyor!")
    return password2

def clean_email(self):
    email = self.cleaned_data.get('email')
    lenghtw = len(User.objects.filter(email=email))
    if lenghtw > 0 :
        raise forms.ValidationError('Bu email adresine sahip bir kullanıcı zaten var.')
    return email

def calisan(request):
queryset = request.user.groups
form = CalisanForm(request.POST or None, group_qs=queryset)
if form.is_valid():
    user =
    group = form.cleaned_data['group']
    username = form.cleaned_data['username']
    member = Member.objects.create(first_name=username)
    group = Group.objects.get(name=form.cleaned_data['group'])
    password = form.cleaned_data.get('password1')
    new_user = authenticate(username=user.username, password=password)
    return redirect('home')
return render(request, 'accounts/calisan/calisan.html', {'form': form, 'title': 'Üye Ol'})

It’s difficult to tell without seeing your models: how User relates to GroupMemberRole.

If user has a “role” FK to GroupMemberRole then you could probably add this before

user.role = form.cleaned_data['role']

There is no corresponding field in my model. The GroupMemberRole has no relationship with the user. In a relationship with the “member”.

If User is related to GroupMemberRole through the Member model then perhaps the following works for you:

    member = Member.objects.create(first_name=username, role=forms.cleaned_data['role'])

It gives an error:

KeyError at /accounts/calisan/calisan

Right, you should change forms.cleaned_data['role'] by forms.cleaned_data['roles'] in my example.

I think I was confused because roles is a ModelChoiceField, for a ModelMultipleChoiceField I would have named it “roles” like you did.

member = Member.objects.create(first_name=username, roles=form.cleaned_data['roles'])

TypeError at /accounts/calisan/calisan
Member() got an unexpected keyword argument ‘roles’

Well, my crystal ball is currently at the garage, but a wild guess would be that you have member = Member.objects.create(first_name=username, roles=form.cleaned_data['roles']) instead of member = Member.objects.create(first_name=username, role=form.cleaned_data['roles'])

However, you will need to post your models (the interesting parts) for an accurate answer: you need to find where to save the role into, what’s the attribute name ? This would work:

member = Member.objects.create(first_name=username, the_good_attribute_name_for_the_foreign_key_to_GroupMemberRole=form.cleaned_data['roles'])

  • replace the_good_attribute_name_for_the_foreign_key_to_GroupMemberRole with the name of the relation from GroupMember to GroupMemberRole