1
votes

I'm getting a ValueError, and I cannot figure the bug out for the life of me. I am aware that the error means there are more values being passed to a variable than expected (ie, (x, y) = 1, 2, 3), but I don't see why that's a problem here.

This is where the error is (in a views.py file):

## 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!!!

This is what the ImageItem model looks like:

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)

This is the information on traceback:

enter image description hereenter image description here

And here are the local variables on the traceback:

enter image description here

Let me know if you guys need any more information, thanks!

EDIT: here is the entire traceback provided:

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

EDIT 2: I'm playing around with it, and there's something strange happening. When I change the line queryset = ImageItem.objects.filter(pk__in=image_pks) to queryset = ImageItem.objects.get(pk=10) (where 10 is an arbitrary id), I get the following error: AttributeError: 'ImageItem' object has no attribute 'get', and the originally problematic line is throwing the error!

It's a strange error, because ImageItem is just a normal model. Of course it has the attribute 'get'. Does this have something to do with fact that the queryset is first being evaluated on the problematic line (formset = ImageInfoFormset(queryset))? Is ImageItem being assigned to something else after the query is written, but before it is evaluated?

2
I doubt that you got the correct line there; there is no variable unpacking taking place on that line. - Martijn Pieters
That's the line that the traceback is signaling (I've changed things around to test, and it keeps telling me the problem is on that line). Isn't there unpacking taking place when the queryset is passed in as an argument to ImageInfoFormset? - sgarza62
Not on that exact line, no. - Martijn Pieters
So what is ImageInfoFormat(queryset) returning? - Peter Rowell
@PeterRowell: That doesn't matter either; the return value is being assigned to one variable. I suspect there is more to this traceback than shown here. - Martijn Pieters

2 Answers

1
votes

Try getting rid of the queryset= definition (your first line) and doing the following instead:

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

You have to explicitly define that you are changing the queryset, otherwise you are just passing another variable to the formset.

See the docs here.

0
votes

I would try with `formset = ImageInfoFormset(queryset.values())

formset = ImageInfoFormset(queryset.values())

instead of

formset = ImageInfoFormset(queryset)