Skip to content
Advertisement

TypeError: __init__() got an unexpected keyword argument ‘choices’

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']

Advertisement

Answer

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']
Advertisement