Django forms错误“关键字参数”“choices”“有多个值”

2024-10-01 17:29:35 发布

您现在位置:Python中文网/ 问答频道 /正文

在定义django表单时出现一个奇怪的错误。我得到了一个错误:

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

TestForm和SpeciesForm(下面引用)都会发生这种情况;基本上这两个表单都带有“choices”关键字参数。init()从未被显式调用,窗体甚至还没有在视图中实例化。有一个模型表单和一个普通表单。在

^{pr2}$

任何帮助都将不胜感激。不知道为什么会发生这种事,因为这些形体都是非常简陋的。在


Tags: django表单for定义init错误情况multiple
1条回答
网友
1楼 · 发布于 2024-10-01 17:29:35

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)

使用:

^{pr2}$

以及:

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

这是假设你的选择是有效的。不知道什么标本.种以及试验.试验是。这些应该是两个元组的iterable,根据:

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.

相关问题 更多 >

    热门问题