【问题标题】:Need 2 values to unpack in for loop; got 1. in Django 3.0需要 2 个值来解压 for 循环;在 Django 3.0 中获得 1.
【发布时间】:2021-12-30 12:15:47
【问题描述】:

在将我的项目从 Django 1.8 更新到 Django 3.0 后,我遇到了这个问题“需要 2 个值才能在 for 循环中解压;得到 1 个”

让我们考虑一下我的views.py:

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())

我该如何解决我的问题

【问题讨论】:

    标签: python-3.x django django-models django-views django-templates


    【解决方案1】:

    来自文档:

    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

    【讨论】:

    • 尝试切换到render()
    【解决方案2】:

    render_to_response() 在 Django 3.0 中被删除,使用 render() 所以最后你的代码看起来像这样

    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() 函数接受doc. 中指定的多个参数

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 1970-01-01
      • 2019-01-09
      • 2019-01-09
      • 2021-02-23
      • 2021-04-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      相关资源
      最近更新 更多