How can i get selected value in user update form?

I want to get my selected job_role which I have selected at the time of user creation.

class EditUserForm(HelperForm):
job_role = forms.ModelChoiceField(queryset=Group.objects.all(), required=True)

    USER_TYPE_CHOICES = (
        (False, 'Employee'),
        (True, 'Admin'),
    )
    is_superuser = forms.ChoiceField(label='User Type', choices=USER_TYPE_CHOICES)

    class Meta:
        model = User
        fields = [
            'first_name', 'last_name', 'username', 'email', 'is_superuser', 'job_role'
        ]

    def __init__(self, *args, **kwargs):
        super(EditUserForm, self).__init__(*args, **kwargs)
        self.fields['job_role'].initial = self.instance.groups.all()


    def save(self, commit=True):
        user = super(EditUserForm, self).save(commit)
        user.save()
        user.groups.clear()
        user.groups.add(self.cleaned_data.get('job_role'))
        return user
1 Like