python – Django表单错误’为关键字参数’选择’获取了多个值”
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Django表单错误’为关键字参数’选择’获取了多个值”,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2137字,纯文字阅读大概需要4分钟。
内容图文
![python – Django表单错误’为关键字参数’选择’获取了多个值”](/upload/InfoBanner/zyjiaocheng/742/08b92cc5ee0c4a788a1264245a5f8d66.jpg)
定义我的django表单时出现一个奇怪的错误.我收到错误:
__init__() got multiple values for keyword argument 'choices'
这种情况发生在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
任何帮助,将不胜感激.不知道为什么会发生这种情况,因为表格非常简单.
解决方法:
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.
内容总结
以上是互联网集市为您收集整理的python – Django表单错误’为关键字参数’选择’获取了多个值”全部内容,希望文章能够帮你解决python – Django表单错误’为关键字参数’选择’获取了多个值”所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。