views:

2548

answers:

4

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)
A: 

If I had to guess, it's because whatever is in the administrative template expects a list of tuples, but you've instead supplied a tuple of tuples (hence the "too many values"). Try 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'),        
]
John Feminella
Django's choices can be any iterable (http://docs.djangoproject.com/en/dev/ref/models/fields/#choices), not just a "list or a tuple", so this isn't likely it, even though it's a good thought.
Jarret Hardie
Ah, darn. Worth a shot!
John Feminella
A: 

Per http://code.djangoproject.com/ticket/972 , you need to move the assignment CATEGORY_CHOICES = ... outside the class statement.

Alex Martelli
A: 

I got it working. Most of the 'too many values to unpack' errors that i came across while googling were Value Error types. My error was a Template Syntax type. To load my recipe table i had imported a csv file. I was thinking maybe there was a problem somewhere in the data that sqlite allowed on import. So i deleted all data and then added 2 recipes manually via django admin form. The list of recipes loaded after that.

thanks.

I have to say, this isn't a terribly satisfying answer. I'd be more interested in knowing how to fix the problem (and thus what it was), not a workaround. After all, what happens if it occurs again? But I'm glad you were able to find a solution that worked for you.
John Feminella
A: 

I just had the same problem... my cvs file came from ms excel and the date fields gotten the wrong format at saving time. I change the format to something like '2010-05-04 13:05:46.790454' (excel gave me 5/5/2010 10:05:47) and voilaaaa no more 'too many values to unpack’

Billy Tobon