django错误“无法解包的值太多” [英] django error 'too many values to unpack'

查看:94
本文介绍了django错误“无法解包的值太多”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过构建一个简单的食谱应用程序来学习Django。我有一个1表模型,其中对食谱类别使用了选择字段选项,而不是使用第二个类别表和外键关系。所以我通过syncdb创建了数据库表,然后用测试数据加载了表。当我去管理并单击食谱链接以尝试查看食谱时,出现以下错误:

I'm learning Django by building a simple recipes app. I have a 1 table model using the 'choices' field option for recipe categories rather than using a 2nd 'categories' table and a foreign key relationship. So i created db table via syncdb and then loaded table with test data. When i go to admin and click on the 'Recipes' link in an attempt to view recipes i get the following error:

Template error

In template /var/lib/python-support/python2.6/django/contrib/admin/templates/admin/change_list.html, error at line 34
Caught an exception while rendering: too many values to unpack

如果有人可以揭开这个神秘的错误,那就太好了。 Db是Sqlite。 Django版本是1.0。该模型在下面列出:

If anyone can shed light on this cryptic error that would be great. Db is Sqlite. Django version is 1.0. The model is listed below:

from django.db import models

class Recipe(models.Model):
    CATEGORY_CHOICES = (
        (1, u'Appetizer'),
        (2, u'Bread'),
        (3, u'Dessert'),
        (4, u'Drinks'),
        (5, u'Main Course'),
        (6, u'Salad'),
        (7, u'Side Dish'),
        (8, u'Soup'),
        (9, u'Sauce/Marinade'),
        (10, u'Other'),        
    )
    name = models.CharField(max_length=255)
    submitter = models.CharField(max_length=40)
    date = models.DateTimeField()
    category = models.SmallIntegerField(choices=CATEGORY_CHOICES)
    ingredients = models.TextField()
    directions = models.TextField()
    comments = models.TextField(null=True, blank=True)


推荐答案

编辑:根据kibibu的更正进行了更新。

我遇到了我相信是同样的错误,并产生以下消息:

I have encountered what I believe is this same error, producing the message:

Caught ValueError while rendering: too many values to unpack

我的表单类如下:

class CalcForm(forms.Form):
    item = forms.ChoiceField(choices=(('17815', '17816')))

请注意,我的选择在此处键入一个元组。 Django官方文档的 choices arg内容如下:

Note that my choices type here a tuple. Django official documentation reads as follows for the choices arg:


可迭代(例如(一个列表或元组)(包含2个元组)作为
此字段的选择。此参数接受与模型字段的
参数相同的格式。

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.

src: https://docs.djangoproject.com/en/1.3/ref/ form / fields /#django.forms.ChoiceField.choices

这个问题是通过查看文档并使用元组的列表解决的:

This problem was solved by my observing the documentation and using a list of tuples:

class CalcForm(forms.Form):
    item = forms.ChoiceField(choices=[('17815', '17816')])

请注意,尽管文档中声明了可以使用正确的格式,2元组的元组不起作用:

Do note that while the docs state any iterable of the correct form can be used, a tuple of 2-tuples did not work:

item = forms.ChoiceField(choices=(('17815', '17816'), ('123', '456')))

此产生了与以前相同的错误。

This produced the same error as before.

教训:发生了错误。

这篇关于django错误“无法解包的值太多”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆