2011-05-02 56 views
3

定义我的django表单时出现奇怪的错误。我得到的错误:Django形式错误'为关键字参数'选项'获得多个值'

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

这与TestForm和SpeciesForm(引用下面);基本上都是用“选择”关键字参数形成的。 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 

任何帮助,将不胜感激。不知道为什么会发生这种情况,因为这些表格是相当简单的。

+3

回溯不只是随机噪声,你知道:它包含有用的调试信息。请张贴它。 – 2011-05-02 18:52:37

+0

@Daniel Roseman:+1。你的评论让我感到震惊。 – 2011-05-02 19:00:13

+0

这是一个奇怪的。我可以一贯地重现它,似乎无法找到解决方法。确实奇怪! – jathanism 2011-05-02 19:04:04

回答

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

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.

+1

谢谢!我认为这个问题突出了表单字段的一些可用性问题(模型和表单在一定程度上)。直觉上,我认为我可以像给任何其他表单域一样给这些标签。 – 2011-05-02 19:06:51

+0

+1,因为你很快! – jathanism 2011-05-02 19:07:30

相关问题