Home > Mobile >  Django says: too many values to unpack (expected 2)
Django says: too many values to unpack (expected 2)

Time:12-19

I'm using Django. In the upload_book function, I generate a 10-digit code for each book and I identify and distinguish two books from one another by their code, rather than their primary key. However, I get an error in the generate_code function:

ValueError at /main/upload_book/
too many values to unpack (expected 2)

Here is the generate_code() function with required imports:

import random, string
def generate_code(max_length):
    code = ''.join(random.choices(string.ascii_letters   string.digits, k=max_length))
    while len(Book.objects.filter(code)) != 0:
        code = ''.join(random.choices(string.ascii_letters   string.digits, k=max_length))
    return code

I did the extra bit over there to prevent two books being of the same code, however low the probability of that might be with 629 possible combinations. And here's my upload_book() function:

@login_required
def upload_book(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        description = request.POST.get('description')
        author = request.POST.get('author')
        code = generate_code(10)
        owner = request.user
        if 'thumbnail' in request.FILES:
            thumbnail = request.FILES['thumbnail']
        book = Book(title=title, description=description, author=author, thumbnail=thumbnail, owner=owner, code=code)
        book.save()
        return redirect("/main/all_books/")
    else:
        return render(request, 'main/upload_book.html')

And my Book model in models.py:

class Book(models.Model):
    title = models.CharField(max_length=1000, blank=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
    description = models.TextField()
    author = models.CharField(max_length=1000, blank=True, null=True)
    thumbnail = models.ImageField(upload_to='book_thumbnails', blank=True)
    creation_date = models.DateTimeField(default=timezone.now)
    status = IntegerField(default=1)
    min_age = models.IntegerField(blank=True, null=True)
    max_age = models.IntegerField(blank=True, null=True)
    code = models.CharField(blank=True, null=True, max_length=10)
    community = models.ForeignKey(Community, blank=True, null=True, on_delete=models.CASCADE)
    def __str__(self):
        return self.title

The error is at this line of the upload_book() function:

code = generate_code(10) 

Which leads to this line in the generate_code() function:

while len(Book.objects.filter(code)) != 0: 

Here's the error with its traceback:

Internal Server Error: /main/upload_book/
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/opt/homebrew/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/homebrew/lib/python3.9/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/adithraghav/Documents/Work/centel/books/main/views.py", line 101, in upload_book
    code = generate_code(10)
  File "/Users/adithraghav/Documents/Work/centel/books/main/views.py", line 24, in generate_code
    while len(Book.objects.filter(code)) != 0:
  File "/opt/homebrew/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/opt/homebrew/lib/python3.9/site-packages/django/db/models/query.py", line 941, in filter
    return self._filter_or_exclude(False, args, kwargs)
  File "/opt/homebrew/lib/python3.9/site-packages/django/db/models/query.py", line 961, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
  File "/opt/homebrew/lib/python3.9/site-packages/django/db/models/query.py", line 968, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "/opt/homebrew/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1393, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/opt/homebrew/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1412, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "/opt/homebrew/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1283, in build_filter
    arg, value = filter_expr
ValueError: too many values to unpack (expected 2)

I don't know why this is happening. In another question, the accepted answer mentioned that this happens when split() is used and that returns more than two elements. I'm joining the elements of the list which is generated by random.choices(string.ascii_letters string.digits, k=max_length) into a string. When I print ''.join(random.choices(string.ascii_letters string.digits, k=max_length)) in the Python IDLE shell, it returns a string all right. So what's the issue here?

CodePudding user response:

When you use filter() method on model objects you have to pass filed name too

So change this line

while len(Book.objects.filter(code)) != 0:

to

while Book.objects.filter(code=code).count() != 0:
  • Related