[Django]-Django ModelChoiceField, to_field_name

3👍

You are confusing fields and widgets. A ModelChoiceField is not a widget, but a form field:

class UserRegister(forms.ModelForm):

    school_code = forms.ModelChoiceField(queryset=MstSchool.objects.filter(use_yn__exact="Y").order_by("code"), empty_label="(Nothing)")

    class Meta:
        model = MstUser
        fields = [ 'name', 'id', 'password', 'school_code']
        widgets = {
            'id' : forms.TextInput(attrs={'placeholder' : 'User ID', 'class':'form-control'}),
            'password' : forms.TextInput(attrs={'placeholder' : 'password', 'class':'form-control school-password', 'type':'password'}),
            'name' : forms.TextInput(attrs={'placeholder' : 'name', 'class':'form-control school-name'}),
        }

Please do not set the id and the password through the ModelForm. Passwords should be hashed, and usually the set_password method [Django-doc] is used for that.

Leave a comment