[英]TypeError: __init__() got an unexpected keyword argument 'choices'
TypeError: init () got an unexpected keyword argument 'choices' TypeError: init () got an unexpected keyword argument 'choices'
forms.py forms.py
class StudentMarksheetform2(forms.Form):
subject_code=(
(1,'CS101'),
(2,'CS102'),
(3,'CS103'),
(4,'CS104'),
(5,'CS105'),
(6,'CS106')
)
code_title=forms.IntegerField(choices=subject_code,default='1')
class Meta():
model=StudentMarksheetdata2
fields=['code_title']
This is a form.这是一种形式。 A form deals with interacting with the user.
表单处理与用户的交互。 An
IntegerField
of forms
has no choices
. forms
的IntegerField
没有choices
。 After all the IntegerField
of models deals with how we store data in the database.毕竟模型的
IntegerField
处理我们如何在数据库中存储数据。
You can use a TypedChoiceField
[Django-doc] for this:您可以为此使用
TypedChoiceField
[Django-doc] :
class StudentMarksheetform2(forms.Form):
SUBJECT_CODE = (
(1,'CS101'),
(2,'CS102'),
(3,'CS103'),
(4,'CS104'),
(5,'CS105'),
(6,'CS106')
)
code_title=forms.
TypedChoiceField(choices=SUBJECT_CODE
, coerce=int, initial=1
)
class Meta:
model=StudentMarksheetdata2
fields=['code_title']
"forms.IntegerField()" doesn't have "choices" and "default" options different from "models.IntegerField()" which has "choices" and "default" options . “forms.IntegerField()”没有“choices”和“default”选项,与“models.IntegerField()”有“choices”和“default”选项不同。
So, to have Dropdown Single Select Box using the fields(classes) from "django.forms" , use "ChoiceField" or "TypedChoiceField" which have "choices" and "initial" options as shown below.因此,要使用“django.forms ”中的字段(类)拥有下拉单 Select 框,请使用具有“选择”和“初始”选项的“ChoiceField”或“TypedChoiceField” ,如下所示。 * "initial" option is equivalent to "default" option :
* “初始”选项等同于“默认”选项:
"ChoiceField" : “选择领域” :
# "forms.py"
from django import forms
class StudentMarksheetform2(forms.Form):
# ...
code_title=forms.ChoiceField(choices=SUBJECT_CODE, initial=1)
# ...
"TypedChoiceField" : “类型选择字段” :
# "forms.py"
from django import forms
class StudentMarksheetform2(forms.Form):
# ...
code_title=forms.TypedChoiceField(choices=SUBJECT_CODE, initial=1)
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.