Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将元组传递到_init__Python_Django_Python 2.7_Django Forms - Fatal编程技术网

在Python中将元组传递到_init_

在Python中将元组传递到_init_,python,django,python-2.7,django-forms,Python,Django,Python 2.7,Django Forms,我有一节这样的课 class AddNoteForm(forms.Form): def __init__(self, test_values, *args): self.custom_choices = test_values super(AddNoteForm, self).__init__() self.fields['choices'] = forms.ModelMultipleChoiceField(label='Test Choi

我有一节这样的课

class AddNoteForm(forms.Form):
    def __init__(self, test_values, *args):
        self.custom_choices = test_values
        super(AddNoteForm, self).__init__()
        self.fields['choices'] = forms.ModelMultipleChoiceField(label='Test Choices', choices=self.custom_choices)
我想在创建类时传递这个元组

test_values = (
    ('name1', 'value1'), 
    ('name2', 'value2'),
    ('name3', 'value3'),
)

form = AddNoteForm(test_values)
但每当我这样做时,我都会得到一个
\uuuu init\uuuuu()至少接受2个参数(给定2个)
错误。我还使用了python 2.7(和Django 1.8)

我查看调试页面中的变量,self.custom_选项包含正确的值(传递到函数中的test_值)


有什么想法吗?

modelmultipechoicefield需要一个queryset作为它的第一个参数(在self之后)。这里需要的是一个规则的多回声场

我还将args/kwargs传递给了超类init,这是一个很好的实践,因为表单可以接受很多有用的参数,比如“initial”,您可能需要使用一些时间,然后当它不工作时会让您发疯

class AddNoteForm(forms.Form):
    def __init__(self, test_values, *args, **kwargs):
        self.custom_choices = test_values
        super(AddNoteForm, self).__init__(*args, **kwargs)
        self.fields['choices'] = forms.MultipleChoiceField(
          label='Test Choices',
          choices=self.custom_choices
        )

forms.Form
的方法签名是什么,
AddNoteForm
从中继承?