gpt4 book ai didi

python - django ModelChoiceField,to_field_name

转载 作者:行者123 更新时间:2023-12-05 03:58:48 26 4
gpt4 key购买 nike

当我编译我的代码时,返回了这条错误消息:

'ModelChoiceField' object has no attribute 'to_field_name'

ModelChoiceFiled 在“学校代码”字段中。

我已经输入了 to_field_name。

class UserRegister(forms.ModelForm):
class Meta:
mstSchool = MstSchool.objects.filter(use_yn__exact="Y").order_by("code")

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'}),
'school_code' : forms.ModelChoiceField(queryset=MstSchool.objects.filter(use_yn__exact="Y").order_by("code"), empty_label="(Nothing)", to_field_name="school_code")
}

'ModelChoiceField' object has no attribute 'to_field_name'

最佳答案

您混淆了字段和小部件。 ModelChoiceField 不是小部件,而是表单域:

class UserRegister(forms.ModelForm):

<b>school_code =</b> 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'}),
}

不要通过ModelForm 设置idpassword。密码应该被散列,通常是 set_password method [Django-doc]用于此目的。

关于python - django ModelChoiceField,to_field_name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57688099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com