Python Django抛出ValueError:在不更改代码的情况下解包的值太多(预期为2个)
我在子文件夹中编写了一个新函数,我的django项目运行良好,现在抛出以下错误:Python Django抛出ValueError:在不更改代码的情况下解包的值太多(预期为2个),python,django,django-models,django-forms,Python,Django,Django Models,Django Forms,我在子文件夹中编写了一个新函数,我的django项目运行良好,现在抛出以下错误: File "C:\Users\me\PycharmProjects\IndicatorAnalyserWebapp\Analyser\Indicators\urls.py", line 4, in <module> from .views import ( File "C:\Users\me\PycharmProjects\IndicatorAnalyserWebapp\Analyser\
File "C:\Users\me\PycharmProjects\IndicatorAnalyserWebapp\Analyser\Indicators\urls.py", line 4, in <module>
from .views import (
File "C:\Users\me\PycharmProjects\IndicatorAnalyserWebapp\Analyser\Indicators\views.py", line 15, in <module>
from .forms import IndicatorForm, SearchForIndicatorMetaData
File "C:\Users\me\PycharmProjects\IndicatorAnalyserWebapp\Analyser\Indicators\forms.py", line 24, in <module>
class IndicatorForm(forms.ModelForm):
File "C:\Users\me\AppData\Local\Continuum\anaconda37\envs\py34\lib\site-packages\django\forms\models.py", line 252, in __new__
opts.field_classes)
File "C:\Users\me\AppData\Local\Continuum\anaconda37\envs\py34\lib\site-packages\django\forms\models.py", line 166, in fields_for_model
formfield = f.formfield(**kwargs)
File "C:\Users\me\AppData\Local\Continuum\anaconda37\envs\py34\lib\site-packages\django\db\models\fields\__init__.py", line 1873, in formfield
return super(IntegerField, self).formfield(**defaults)
File "C:\Users\me\AppData\Local\Continuum\anaconda37\envs\py34\lib\site-packages\django\db\models\fields\__init__.py", line 872, in formfield
defaults['choices'] = self.get_choices(include_blank=include_blank)
File "C:\Users\me\AppData\Local\Continuum\anaconda37\envs\py34\lib\site-packages\django\db\models\fields\__init__.py", line 802, in get_choices
for choice, __ in choices:
ValueError: too many values to unpack (expected 2)
forms.py
调用模型指示符
,但是这与上次提交时的情况没有改变。我选择的所有字段都是元组,因此不应抛出此错误。如有任何建议,将不胜感激
编辑-同时添加模型
class Indicator(models.Model):
time_periods = (
get_time_periods()
)
months = (
get_months()
)
calculations = (
get_calculations()
)
sex_ids = (
get_all_sexes()
)
age_ids = (
get_all_ages()
)
operands = (
('+', '+'),
('-', '-'),
('*', '*'),
('/', '/')
)
salt_options = get_salt_options_as_tuple()
qof_data_choices = (
('qof-{}-prev-ach-exc-ms-prac.xlsx', 'Musculoskeletal group'),
('qof-{}-prev-ach-exc-neu-prac.xlsx', 'Mental health and neurology group'),
('qof-{}-prev-ach-exc-fer-obs-gyn-prac.xlsx', 'Fertility, obstetrics and gynaecology group'),
('qof-{}-prev-ach-exc-hd-prac.xlsx', 'High dependency and other long term conditions group'),
('qof-{}-prev-ach-exc-ls-prac.xlsx', 'Lifestyle group'),
('qof-{}-prev-ach-exc-resp-prac.xlsx', 'Respiratory group'),
('qof-{}-prev-ach-exc-cv-prac.xlsx', 'Cardiovascular group'),
('qof-{}-prac-dom-ach.xlsx', 'Achievement group'),
('qof-{}-prac-dom-exc.xlsx', 'Exclusion group'),
)
id = models.IntegerField(unique=True, primary_key=True)
source_data_family = models.ForeignKey(IndicatorFamily, on_delete=models.CASCADE, blank=True)
qof_source_data = models.CharField(max_length=100, choices=qof_data_choices, blank=True)
source_sheet = models.CharField(max_length=200, blank=True)
year_range = models.CharField(max_length=2, blank=True)
time_period = models.CharField(max_length=30, choices=time_periods, blank=True)
ageId = models.IntegerField(choices=age_ids, blank=True)
sexId = models.IntegerField(choices=sex_ids)
calculation = models.CharField(max_length=30, choices=calculations, blank=True)
rate = models.IntegerField(blank=True, null=True)
value_declared_independently = models.BooleanField(default=False)
value_column_name = models.CharField(max_length=200, blank=True)
numerator_starts_with_financial_year = models.BooleanField(default=False)
numerator = models.CharField(max_length=200, blank=True)
salt_numerator = models.CharField(max_length=200, blank=True, choices=salt_options)
salt_numerator_operand = models.CharField(max_length=2, choices=operands, blank=True)
salt_numerator_2 = models.CharField(max_length=200, blank=True, choices=salt_options)
numerator_minus_not_caseness = models.BooleanField(default=False)
iapt_q_m_variable_type = models.CharField(max_length=200, blank=True)
iapt_q_m_variable_a = models.CharField(max_length=200, blank=True)
iapt_q_m_variable_b = models.CharField(max_length=200, blank=True)
iapt_q_m_variable_type_denominator = models.CharField(max_length=200, blank=True)
iapt_q_m_variable_a_denominator = models.CharField(max_length=200, blank=True)
iapt_q_m_variable_b_denominator = models.CharField(max_length=200, blank=True)
denominator_starts_with_financial_year = models.BooleanField(default=False)
denominator = models.CharField(max_length=200, blank=True)
denominator_minus_not_caseness = models.BooleanField(default=False)
denominator_source_sheet = models.CharField(max_length=200, blank=True)
salt_denominator = models.CharField(max_length=200, blank=True, choices=salt_options)
salt_denominator_operand = models.CharField(max_length=2, choices=operands, blank=True)
salt_denominator_2 = models.CharField(max_length=200, blank=True, choices=salt_options)
add_denominator_column = models.CharField(max_length=200, blank=True)
additional_denominator_column_starts_with_financial_year = models.BooleanField(default=False)
adjust_denominator_for_person_years = models.BooleanField(default=False)
ccg_over_18_population_as_denominator = models.BooleanField(default=False)
ccg_total_population_as_denominator = models.BooleanField(default=False)
is_count = models.BooleanField(default=False)
is_percentage = models.BooleanField(default=False)
ccg_to_stp = models.BooleanField(default=False)
gp_to_upper_tier_la = models.BooleanField(default=False)
ccg_lookup_required = models.BooleanField(default=False)
drop_depression = models.BooleanField(default=False)
count_non_nulls = models.BooleanField(default=False)
non_null_columns = models.CharField(max_length=300, blank=True)
drop_eng_row = models.BooleanField(default=False)
months_to_quarter = models.BooleanField(default=False)
snapshot = models.BooleanField(default=False)
asterisk_as_two = models.BooleanField(default=False)
creator = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
denominator_2_equals_denominator = models.BooleanField(default=False)
do_not_suppress_small_numbers = models.BooleanField(default=False)
numerator_is_num_minus_denom = models.BooleanField(default=False)
stp_is_average_not_sum = models.BooleanField(default=False)
mental_health_trust_geographies = models.BooleanField(default=False)
filter_by = models.CharField(max_length=200, blank=True)
# ld_denominator_source_file = models.CharField(max_length=100, choices=qof_data_choices, blank=True)
def __str__(self):
return str(self.id)
编辑2-更新的回溯:
File "C:\Users\me\PycharmProjects\IndicatorAnalyserWebapp\Analyser\Indicators\urls.py", line 4, in <module>
from .views import (
File "C:\Users\me\PycharmProjects\IndicatorAnalyserWebapp\Analyser\Indicators\views.py", line 15, in <module>
from .forms import IndicatorForm, SearchForIndicatorMetaData
File "C:\Users\me\PycharmProjects\IndicatorAnalyserWebapp\Analyser\Indicators\forms.py", line 19, in <module>
class IndicatorForm(forms.ModelForm):
File "C:\Users\me\AppData\Local\Continuum\anaconda37\envs\py34\lib\site-packages\django\forms\models.py", line 252, in __new__
opts.field_classes)
File "C:\Users\me\AppData\Local\Continuum\anaconda37\envs\py34\lib\site-packages\django\forms\models.py", line 166, in fields_for_model
formfield = f.formfield(**kwargs)
File "C:\Users\me\AppData\Local\Continuum\anaconda37\envs\py34\lib\site-packages\django\db\models\fields\__init__.py", line 1873, in formfield
return super(IntegerField, self).formfield(**defaults)
File "C:\Users\me\AppData\Local\Continuum\anaconda37\envs\py34\lib\site-packages\django\db\models\fields\__init__.py", line 872, in formfield
defaults['choices'] = self.get_choices(include_blank=include_blank)
File "C:\Users\me\AppData\Local\Continuum\anaconda37\envs\py34\lib\site-packages\django\db\models\fields\__init__.py", line 802, in get_choices
for choice, __ in choices:
ValueError: too many values to unpack (expected 2)
选项
应采用以下格式:
[
(value_1, label_1),
(value_2, label_2),
...
]
您需要修改代码,以将项目以2元组格式附加到列表中:
ids.append((indicator.id, indicator.id))
上面,indicator.id
也是选择的标签。标签是django在选择选项时向用户显示的内容。您可能应该提供对象的名称,而不是普通id
注意:威廉的另一个答案对你的情况来说是一个更好的解决方案 我选择的所有字段都是元组,因此不应抛出此错误 它抛出一个错误,因为您在此处向表单中添加了一个字段,其中包含:
... = forms.ChoiceField(choices=get_indicator_ids())
但是选择字段必须是元组的元组,才能在@Willem Van Onsem回复。我已经实现了这些更改,但仍然得到相同的错误。@RustyBrain:那么可能仍然有一个字段的选项使用平面列表,而不是2元组列表。@RustyBrain:您确实删除了
\uuu init\uuu
函数中ChoiceField
的构造,对吗?是的,我确实对整个课堂进行了注释,并用你的建议替换了它。@RustyBrain:看起来仍然有一些选项字段
s等,以平面列表/元组作为选项。错误是否仍然位于类指示符表单(forms.ModelForm):
(在回溯中)?谢谢您的回答。如上所述,我已经尝试过了,但仍然得到相同的错误
ids.append((indicator.id, indicator.id))
... = forms.ChoiceField(choices=get_indicator_ids())
class IndicatorDropDown(forms.Form):
indicators = forms.ModelChoiceField(queryset=Indicator.objects.all())