Home > Software design >  Need 2 values to unpack in for loop; got 1. in Django 3.0
Need 2 values to unpack in for loop; got 1. in Django 3.0

Time:12-31

After updating my project from Django 1.8 to Django 3.0 i am facing this issue "Need 2 values to unpack in for loop; got 1 "

Let us consider my views.py as:

def add_items(request, pk, ot):
    client = request.user.client
    val1 = []
    warehouse = [
        (str(use.pk), use.name)
        for use in WareHouse.objects.filter(client_id=client).
        exclude(is_active=False)]

    project = Project.objects.filter(client=request.user.client).exclude(is_deleted=True)
    data1 = OtherOrder.objects.filter(id=pk)

    if data1.exists() and (len(warehouse) == 1):
        order = data1[0]
        if order.warehouse is None:
            try:
                order.warehouse_id = warehouse[0][0]
                order.save()
            except BaseException as e:
                logger.exception(e)

    data = OtherOrderItem.objects.filter(other_order_id=pk)
    total = data.aggregate(Sum('total_cost')).get('total_cost__sum') or 0.00
    charges = OtherOrderAdditionalCharges.objects.filter(order_id=pk)
    if charges.filter(charges_calculated=False):
        flag = True

    val = [i.id for i in data]
    for j in val:
        data3 = Movements.objects.values('other_order_item','damaged_quantity').filter(other_order_item=j, other_order_id=pk).annotate(Sum('quantity'))
        val1.append(data3)
    back = '/stock/other_orders/'
    return render(request,'stock/add_items.html',locals())

How could I solve my issue

CodePudding user response:

From docs:

render_to_response(template_name, context=None, content_type=None, status=None, using=None)

Deprecated since version 2.0.

This function preceded the introduction of render() and works similarly except that it doesn’t make the request available in the response.

https://docs.djangoproject.com/en/2.2/topics/http/shortcuts/#render

CodePudding user response:

render_to_response() was removed in Django 3.0 use render() so finally your code will look like this

class StockPricingView(View):
    def get(self, request, pk):
        data1 = OtherOrder.objects.get(id=pk)
        data = PricingModule.objects.filter(item__other_order_id=pk)
        charges = OtherOrderAdditionalCharges.objects.filter(order_id=pk)
        if charges.exists():
            total_charges = charges.aggregate(Sum('amount')).get('amount__sum') or 0.00
            order_total = data1.otherorderitem_set.all().aggregate(Sum('total_cost')).get('total_cost__sum') or 0.00
            per_charges = (total_charges/order_total)*100
        return render(request, 'stock/otherorders/stock_pricing_view.html')

render() function takes multiple arguments specified in doc.

  • Related