定义django表单时出现奇怪的错误。我收到错误:
__init__() got multiple values for keyword argument 'choices'
这与TestForm和SpeciesForm一起发生(引用如下);基本上两种形式都带有'choices'关键字参数。永远不会显式调用 init (),甚至还没有在视图中实例化表单。有一个ModelForm和一个普通表格。
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
任何帮助将不胜感激。不知道为什么会发生这种情况,因为表格非常简单。
答案 0 :(得分:7)
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, *args, **kwargs)
使用:
species = f.ChoiceField(label='Species', choices=Specimen.SPECIES)
tests = f.MultipleChoiceField(label='Test', choices=Test.TESTS, widget=f.CheckboxSelectMultiple())
和
sample_type = f.ChoiceField(label='Type of sample', choices=TYPE_CHOICES)
这假设您的选择有效。不确定Specimen.SPECIES和Test.TESTS是什么。那些应该是可以迭代的两元组根据:
ChoiceField.choices
可迭代(例如,列表或元组) 2元组用作此选项 领域。这个论点接受同样的观点 格式作为a的选择参数 模型领域。请参阅模型字段 关于选择的参考文献 更多细节。