Jos*_*eda 3 python forms django
定义我的django表单时出现一个奇怪的错误.我收到错误:
__init__() got multiple values for keyword argument 'choices'
Run Code Online (Sandbox Code Playgroud)
这种情况发生在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
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.不知道为什么会发生这种情况,因为表格非常简单.
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)
Run Code Online (Sandbox Code Playgroud)
使用:
species = f.ChoiceField(label='Species', choices=Specimen.SPECIES)
tests = f.MultipleChoiceField(label='Test', choices=Test.TESTS, widget=f.CheckboxSelectMultiple())
Run Code Online (Sandbox Code Playgroud)
和:
sample_type = f.ChoiceField(label='Type of sample', choices=TYPE_CHOICES)
Run Code Online (Sandbox Code Playgroud)
这假设您的选择有效.不确定Specimen.SPECIES和Test.TESTS是什么.那些应该是可以迭代的两元组根据:
ChoiceField.choices
2元组的可迭代(例如,列表或元组),用作此字段的选项.此参数接受与模型字段的choices参数相同的格式.有关详细信息,请参阅有关选项的模型字段参考文档.
| 归档时间: |
|
| 查看次数: |
2698 次 |
| 最近记录: |