Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

python - django error 'too many values to unpack'

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

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Edit: Updated in light of kibibu's correction.

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

Caught ValueError while rendering: too many values to unpack

My form class was as follows:

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

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

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/forms/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')])

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.

Lesson: bugs happen.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...