[ACCEPTED]-django error 'too many values to unpack'-sqlite
Edit: Updated in light of kibibu's correction.
I have encountered what I believe is this 13 same error, producing the message:
Caught ValueError while rendering: too many values to unpack
My form 12 class was as follows:
class CalcForm(forms.Form):
item = forms.ChoiceField(choices=(('17815', '17816')))
Note that my choices
type 11 here a tuple. Django official documentation 10 reads as follows for the choices
arg:
An iterable 9 (e.g., a list or tuple) of 2-tuples to use 8 as choices for this field. This argument 7 accepts the same formats as the choices argument 6 to a model field.
src: https://docs.djangoproject.com/en/1.3/ref/forms/fields/#django.forms.ChoiceField.choices
This problem was solved 5 by my observing the documentation and using 4 a list of tuples:
class CalcForm(forms.Form):
item = forms.ChoiceField(choices=[('17815', '17816')])
Do note that while the docs state any 3 iterable of the correct form can be used, a 2 tuple of 2-tuples did not work:
item = forms.ChoiceField(choices=(('17815', '17816'), ('123', '456')))
This produced 1 the same error as before.
Lesson: bugs happen.
You should use a ChoiceField
instead of SmallIntegerField
0
If I had to guess, it's because whatever 4 is in the administrative template expects 3 a list of tuples, but you've instead supplied 2 a tuple of tuples (hence the "too many values"). Try 1 replacing with a list instead:
CATEGORY_CHOICES = [ # Note square brackets.
(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'),
]
Per http://code.djangoproject.com/ticket/972 , you need to move the assignment CATEGORY_CHOICES = ...
outside the 1 class
statement.
I got it working. Most of the 'too many 9 values to unpack' errors that i came across 8 while googling were Value Error types. My 7 error was a Template Syntax type. To load 6 my recipe table i had imported a csv file. I 5 was thinking maybe there was a problem somewhere 4 in the data that sqlite allowed on import. So 3 i deleted all data and then added 2 recipes 2 manually via django admin form. The list 1 of recipes loaded after that.
thanks.
I just had the same problem... my cvs file 6 came from ms excel and the date fields gotten 5 the wrong format at saving time. I change 4 the format to something like '2010-05-04 3 13:05:46.790454' (excel gave me 5/5/2010 2 10:05:47) and voilaaaa no more 'too many 1 values to unpack’
kibibu's comment to Kreychek's answer is 20 correct. This isn't a Django issue but rather 19 an interesting aspect of Python. To summarize:
In 18 Python, round parentheses are used for both 17 order of operations and tuples. So:
foo = (2+2)
will 16 result in foo being 4, not a tuple who's 15 first and only element is 4: 4
foo = (2+2, 3+3)
will result 14 in foo being a two-dimensional tuple: (4,6)
To 13 tell Python that you want to create a one-dimensional 12 tuple instead of just denoting the order 11 of operations, use a trailing comma:
foo = (2+2,)
will 10 result in foo being a one-dimensional tuple 9 who's first and only element is 4: (4,)
So 8 for your scenario:
class CalcForm(forms.Form):
item = forms.ChoiceField(choices=(('17815', '17816'),))
would give what you want. Using 7 a list is a great solution too (more Pythonic 6 in my opinion), but hopefully this answer 5 is informative since this can come up in 4 other cases.
For example:
print("foo: %s" % (foo))
may give an error 3 if foo is an iterable, but:
print("foo: %s" % (foo,))
or:
print("foo: %s" % [foo])
will properly 2 convert foo to a string whether it's an 1 iterable or not.
Documentation: http://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences
This error IMO occurs when a function returns 2 three values, and you assign it to 2. e.g.:
def test():
a=0
b=0
c=0
....
a,b=test() <---three values returned, but only assigning to 2.
a,b,c=test() <---three 1 values returned, assigned to 3.OK
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.