加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Django表单错误’为关键字参数’选择’获取了多个值”

发布时间:2020-12-20 11:18:36 所属栏目:Python 来源:网络整理
导读:定义我的 django表单时出现一个奇怪的错误.我收到错误: __init__() got multiple values for keyword argument 'choices' 这种情况发生在TestForm和SpeciesForm上(引用如下);基本上两种形式都带有’choices’关键字参数.永远不会显式调用init(),甚至还没有
定义我的 django表单时出现一个奇怪的错误.我收到错误:

__init__() got multiple values for keyword argument 'choices'

这种情况发生在TestForm和SpeciesForm上(引用如下);基本上两种形式都带有’choices’关键字参数.永远不会显式调用init(),甚至还没有在视图中实例化表单.有一个ModelForm和一个普通Form.

from django import forms as f
from orders.models import *

class TestForm(f.Form):
    species = f.ChoiceField('Species',choices=Specimen.SPECIES)
    tests = f.MultipleChoiceField('Test',choices=Test.TESTS,widget=f.CheckboxSelectMultiple())
    dna_extraction = f.CharField('DNA extraction',help_text='If sending pre-extracted DNA,we require at least 900 ng')

class SpeciesForm(f.ModelForm):
    TYPE_CHOICES = (
        ('blood','Blood'),('dna','Extracted DNA'),)
    dam_provided = f.BooleanField('DAM',help_text='Is dam for this specimen included in sample shipment?')
    sample_type = f.ChoiceField('Type of sample',choices=TYPE_CHOICES)
    dna_concentration = f.CharField('DNA concentration',help_text='If sending extracted DNA,approximate concentration')

    class Meta:
        exclude = ['order']
        model = Specimen

任何帮助,将不胜感激.不知道为什么会发生这种情况,因为表格非常简单.

解决方法

http://code.djangoproject.com/browser/django/trunk/django/forms/fields.py#L647

647     def __init__(self,choices=(),required=True,widget=None,label=None,648                  initial=None,help_text=None,*args,**kwargs):
649         super(ChoiceField,self).__init__(required=required,widget=widget,label=label,650                                         initial=initial,help_text=help_text,**kwargs)

使用:

species = f.ChoiceField(label='Species',choices=Specimen.SPECIES)
tests = f.MultipleChoiceField(label='Test',widget=f.CheckboxSelectMultiple())

和:

sample_type = f.ChoiceField(label='Type of sample',choices=TYPE_CHOICES)

这假设您的选择有效.不确定Specimen.SPECIES和Test.TESTS是什么.那些应该是可以迭代的两元组根据:

ChoiceField.choices

An iterable (e.g.,a list or tuple) of 2-tuples to use as choices for this field. This argument accepts the same formats as the choices argument to a model field. See the model field reference documentation on choices for more details.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读