Valueerror: Too Many Values to Unpack (Expected 2) in Django

ValueError: too many values to unpack (expected 2) in Django

This error would only occur if split() returns more than 2 elements:

app_label, model_name = model.split(".")
ValueError: too many values to unpack (expected 2)

This means that either app_label or model_name has a dot (.) in it. My money is on the former as model names are automatically generated

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

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:

Django and HTML : Too many values to unpack (expected 2)

You code is ver wired...

Choice fields expect to obtain a tuple or a list type: key - value

to fix too many values to unpack (expected 2) change this lines in your form:

typeOfTheproject = forms.ChoiceField(choices = listProject)
wantedFramework = forms.MultipleChoiceField(choices = listFramework)

for:

FIRST VERSION CODE:

class ConfiguratorForm(forms.Form):
queryOfProject = TypeOfProgram.objects.values_list('name')
queryOfFramework = Framework.objects.values_list('name', 'version')
listOfProject = []
listOfFramework = []
listOfFramework += queryOfFramework
listOfProject += queryOfProject
listFramework = []
listProject = []

for i in range(0,len(listOfFramework)):
listFramework.append(listOfFramework[i]['name'] + " " +listOfFramework[i]['version'])
for i in range(0,len(listOfProject)):
listProject.append(listOfProject[i]['name'])

typeOfTheproject = forms.ChoiceField(choices=[(x, x) for x in listProject]) # <---
wantedFramework = forms.MultipleChoiceField(choices=[(x, x) for x in listFramework]) # <---

SECOND VERSION CODE:

class ConfiguratorForm(forms.Form):
queryOfProject = TypeOfProgram.objects.values_list('name')
queryOfFramework = Framework.objects.values_list('name','version')
listFramework = []
listProject = []
listFramework=[(q['name'], q['version'])for q in queryOfFramework] # <---
listProject =[(q['name'])for q in queryOfProject] # <---

print("list of")
print(listFramework)
print(listProject)
typeOfTheproject = forms.ChoiceField(choices = listProject)
wantedFramework = forms.MultipleChoiceField(choices = listFramework)

ValueError: too many values to unpack (expected 2) Django

Your view is returning a QuerySet, but that does not make much sense: a view should return a HttpResponse. You can for example render a template, convert it to JSON, etc.

We can for example render a template with:

from django.shortcuts import render

def otherUserList(request):
userName = request.GET.get('userName', None)
qs = UserList.objects.filter(user__username=userName)
return render(request, 'some-template.html', {'lists': qs})

Or we can for example return a JsonResponse wit the list_name`s of user the user with the given username with:

from django.http import JsonField

def otherUserList(request):
userName = request.GET.get('userName', None)
qs = UserList.objects.filter(user__username=userName)
return JsonResponse({'listnames': [list.name for list in qs]})

ValueError too many values to unpack (expected 2) in django

get_object_or_404 should be passed keyword arguments for the query parameters

parlourdetails = get_object_or_404(Parlour, slug=slug)

Just passing slug as a positional argument does not make sense, Django would not know what field to query against

ValueError: too many values to unpack (expected 2) in Django Window function

You have written:

queryset = Installment.objects.annotate(total=Window(expression=Sum('installment_amount'), order_by=F('installment_month').asc(), frame=RowRange(end=0)))
context['total'] = queryset

As indicated by the variable name queryset is a QuerySet, i.e. a collection of Installment instances. Yet later you write total|get_item:'installment_amount__sum' which effectively translates to queryset.get('installment_amount__sum') which doesn't make sense and obviously gives you an error.

Instead you want to add this window expression to the queryset returned by get_queryset instead, also you want to use order_by on the installment_date ordering on a string month doesn't make much sense:

class InstallmentListView(ListView):
model = Installment
template_name = 'client_management_system/installment_detail.html'
context_object_name = 'installments'

# This function is to show the installments of the client, the details of which we are seeing currently, and
# pk=self.kwargs['pk'] is to get the client id/pk from URL
def get_queryset(self):
user = get_object_or_404(Client, pk=self.kwargs['pk'])
return Installment.objects.filter(client=user).annotate(
total=Window(
expression=Sum('installment_amount'),
order_by=F('installment_date').asc(),
frame=RowRange(end=0)
)
)

# Remove your `get_context_data`

Next in the template you can simply write:

{% for installment in installments %}
<fieldset>
<tr>
<td>{{ installment.installment_date|date:"F d, Y" }}</td>
<td>{{ installment.installment_month }}</td>
<td>{{ installment.installment_amount }}</td>
<td>{{ installment.total }}</td>
</tr>
</fieldset>
{% endfor %}

Note: Why do you even store installment_month you can get that from installment_date... Plus that custom template tag of yours was
unnecessary, dictionary lookups can be done in the template as simply
as {{ variable.key_name }}



Related Topics



Leave a reply



Submit