My code:
groups = forms.ModelChoiceField(queryset=Group.objects.all(), required=False, widget=forms.RadioSelect)
Error:
‘Group’ object is not iterable
Complete view:
class FormModificaUtenteAdmin(forms.ModelForm):
avatar = forms.ImageField(),
date_of_birth = forms.DateField(label=_('Date of birth'), widget=XYZ_DateInput())
groups = forms.ModelChoiceField(queryset=Group.objects.all(), required=False, widget=forms.RadioSelect)
captcha = MathCaptchaField()
class Meta:
model = User
fields = ('first_name','last_name','username','email', 'display_name','info', 'avatar', 'sex' , 'date_of_birth', 'groups', 'user_permissions', 'is_staff', 'is_superuser')
widgets = {
'first_name': forms.TextInput(attrs={'class': 'form-control'}),
'last_name': forms.TextInput(attrs={'class': 'form-control'}),
'username': forms.TextInput(attrs={'class': 'form-control'}),
'email': forms.TextInput(attrs={'class': 'form-control'}),
'info': forms.Textarea(attrs={'class': 'form-control', 'style': 'width: 100%; height: 60%;'}),
'is_staff': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
'is_superuser': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
}
def __init__(self, *args, **kwargs):
super(FormModificaUtenteAdmin, self).__init__(*args, **kwargs)
self.fields['first_name'].required = True
self.fields['last_name'].required = True
self.fields['username'].required = True
self.fields['email'].required = True
self.fields['status'] = forms.ChoiceField(required=False,widget=forms.RadioSelect(attrs={'class': 'form-select'}))
self.fields['sex'] = forms.ChoiceField(choices=sex,required=False,widget=forms.RadioSelect(attrs={'class': 'form-select'}))
self.fields['display_name'] = forms.ChoiceField(choices=display_name,required=False,widget=forms.Select(attrs={'class': 'form-select'}))
self.fields['first_name'].label = _("First name")
self.fields['last_name'].label = _("Last name")
self.fields['username'].label = _("Username")
self.fields['sex'].label = _("Sex")
self.fields['display_name'].label = _("Display name")
self.fields['avatar'].label = _("Pictures")
My screenshot:
How to fixed error??
My views.py:
class ModificaUtente(GroupRequiredMixin, UpdateView):
login_required = True
group_required = [u"manager", u"admin"]
model = User
form_class = FormModificaUtenteAdmin
template_name = 'user_update.html'
def get_success_url(self):
return reverse_lazy('ModificaUtente', kwargs={'pk': self.object.pk})
def get_object(self, queryset=None):
return User.objects.get(pk=self.kwargs['pk'])
Thanks