简体   繁体   English

TypeError: __init__() 得到了一个意外的关键字参数“选择”

[英]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 . formsIntegerField没有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.

相关问题 TypeError at '' __init__() 得到一个意外的关键字参数 '' - TypeError at '' __init__() got an unexpected keyword argument '' TypeError: __init__() 在 python 的 argparser 中得到了一个意外的关键字参数 'choices' - TypeError: __init__() got an unexpected keyword argument 'choices' in python's argparser Django:__init__() 得到了一个意外的关键字参数“选择” - Django: __init__() got an unexpected keyword argument 'choices' TypeError:__init __()为关键字参数“ choices”获得了多个值 - TypeError: __init__() got multiple values for keyword argument 'choices' Scrapy错误:TypeError:__ init __()得到一个意外的关键字参数'deny' - Scrapy Error: TypeError: __init__() got an unexpected keyword argument 'deny' TypeError:__init __()得到了意外的关键字参数错误 - TypeError: __init__() got an unexpected keyword argument error TypeError:__ init __()在argparse中有一个意外的关键字参数'type' - TypeError: __init__() got an unexpected keyword argument 'type' in argparse 图片/创建时发生TypeError-__init __()得到了意外的关键字参数'save' - TypeError at images/create - __init__() got an unexpected keyword argument 'save' TypeError:__init __()获得了意外的关键字参数'n_components' - TypeError: __init__() got an unexpected keyword argument 'n_components' TypeError:__init __()获得了意外的关键字参数'cv' - TypeError: __init__() got an unexpected keyword argument 'cv'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM