2011-05-02 27 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

相關問題