gpt4 book ai didi

python - Django:ValueError:太多值无法解压

转载 作者:太空宇宙 更新时间:2023-11-03 19:06:21 25 4
gpt4 key购买 nike

我遇到了 ValueError,但我一辈子都无法找出这个错误。我知道该错误意味着传递给变量的值比预期多(即 (x, y) = 1, 2, 3),但我不明白为什么这是一个问题在这里。

这是错误所在(在views.py 文件中):

## image_pks is just a list of ImageItem primary keys (eg, [1, 2] or [5])
queryset = ImageItem.objects.filter(pk__in=image_pks)
ImageInfoFormset = modelformset_factory(ImageItem, fields=('title', 'caption'), extra=0)
formset = ImageInfoFormset(queryset) ## Error happening here!!!

这就是 ImageItem 模型的样子:

class ImageItem(models.Model):
user = models.ForeignKey(User)
upload_date = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
library = models.ForeignKey(Library)
img_big = models.ImageField(upload_to=img_get_file_path)
img_363 = models.ImageField(upload_to=img_get_file_path)
title = models.CharField(max_length=50,
blank=True)
caption = models.CharField(max_length=1000,
blank=True)

这是有关回溯的信息:

enter image description here enter image description here

这是回溯上的局部变量:

enter image description here

如果你们需要更多信息,请告诉我,谢谢!

编辑:这是提供的完整回溯:

Traceback:
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/sgarza62/riotry_master/riotry/backboard_general/views.py" in Backboard_Home
44. formset = ImageInfoFormset(queryset) # TOO MANY VALUES ERROR HERE!
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/models.py" in __init__
424. super(BaseModelFormSet, self).__init__(**defaults)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/formsets.py" in __init__
50. self._construct_forms()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/formsets.py" in _construct_forms
114. for i in xrange(self.total_form_count()):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/formsets.py" in total_form_count
88. return self.management_form.cleaned_data[TOTAL_FORM_COUNT]
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/formsets.py" in _management_form
74. if not form.is_valid():
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
124. return self.is_bound and not bool(self.errors)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/forms.py" in _get_errors
115. self.full_clean()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
270. self._clean_fields()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/forms.py" in _clean_fields
281. value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/widgets.py" in value_from_datadict
205. return data.get(name, None)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py" in get
358. clone = self.filter(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py" in filter
624. return self._filter_or_exclude(False, *args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
642. clone.query.add_q(Q(*args, **kwargs))
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
1250. can_reuse=used_aliases, force_having=force_having)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_filter
1056. arg, value = filter_expr

Exception Type: ValueError at /backboard/
Exception Value: too many values to unpack

编辑 2:我正在摆弄它,然后发生了一些奇怪的事情。当我将行 queryset = ImageItem.objects.filter(pk__in=image_pks) 更改为 queryset = ImageItem.objects.get(pk=10) (其中 10 是任意值) id),我收到以下错误:AttributeError:'ImageItem'对象没有属性'get',并且最初有问题的行抛出错误!

这是一个奇怪的错误,因为 ImageItem 只是一个普通模型。当然它有属性'get'。这是否与查询集首先在有问题的行上求值(formset = ImageInfoFormset(queryset))有关?在编写查询之后、评估之前,ImageItem 是否被分配给其他内容?

最佳答案

尝试删除 queryset= 定义(第一行)并执行以下操作:

ImageInfoFormset = modelformset_factory(ImageItem, fields=('title', 'caption'), extra=0)
formset = ImageInfoFormset(queryset=ImageItem.objects.filter(pk__in=image_pks))

您必须明确定义要更改查询集,否则您只是将另一个变量传递给表单集。

请参阅文档 here.

关于python - Django:ValueError:太多值无法解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14632877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com