TypeError: __init__() got an unexpected keyword argument 'choices'
Question:
TypeError: __init__() got an unexpected keyword argument 'choices'
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']
Answers:
This is a form. A form deals with interacting with the user. An IntegerField
of forms
has no choices
. After all the IntegerField
of models deals with how we store data in the database.
You can use a TypedChoiceField
[Django-doc] for this:
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.
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. *"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)
# ...
TypeError: __init__() got an unexpected keyword argument 'choices'
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
. After all the IntegerField
of models deals with how we store data in the database.
You can use a TypedChoiceField
[Django-doc] for this:
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.
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. *"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)
# ...