当数据库中没有数据时,隐藏 - 不支持的操作数类型:“int”和“NoneType”

问题描述 投票:0回答:0

除非数据库中至少有 2 行数据,否则我会遇到此错误。基本上我所做的是将商品标记为已退款。我将汇总所有字段,然后进行计算。

如果我在数据库中没有至少 2 行数据,我总是会得到这个错误。

unsupported operand type(s) for -: 'int' and 'NoneType'

具体报错的行是:

total_partial = 0 - total_p_r['refund_amount__sum'] 

我不明白为什么当模型的所有行的默认设置为零时我得到 NoneType。代码如下。

views.py

def inventory_management_refund(request):
    inventory = Inventory.objects.filter(Q(status__contains='REFUNDED') | Q(status__contains='Partial Refund')).order_by('id')
        
    totals = Inventory.objects.filter(status__contains='REFUNDED').aggregate(
        Sum('paid'),
        Sum('soldprice'),
        Sum('shipcost'),
        )
        
    total_p_r = Inventory.objects.filter(status__contains='Partial Refund').aggregate(
        Sum('refund_amount')
        )

    total_full = 0 - totals['paid__sum'] - totals['soldprice__sum'] - totals['shipcost__sum']
    total_partial = 0 - total_p_r['refund_amount__sum']

    total_refund_loss = total_full + total_partial

    return render(request, 'inventory/refunds.html', {"inventory": inventory, "total_refund_loss": total_refund_loss})

template.html

<div>
  <div>
    <h1 class="text-center">Refunded Items</h1>
    <hr>
  </div>
  {% if inventory.count == 0 %}
  <h2 class="text-success text-center">There are currently no refunds!</h2>
  {% endif %}
    {% if inventory.count > 0 %}
    <table class="table table-striped">
      <thead>
         <tr>
           <th>Product</th>
           <th>Description</th>
           <th class="invmanrow">Purchase Price</th>
           <th class="invmanrow">Sold Price</th>
           <th class="invmanrow">Ship Cost</th>
           <th class="invmanrow">Refund Amount</th>
           <th>Status</th>
          </tr>
      </thead>
      <tbody>
      {% for inventory in inventory %}
      <tr>
       <td>{{inventory.product}}</td>
       <td>{{inventory.description}}</td>
       <td class="invmanrow">{{inventory.paid}}</td>
       <td class="invmanrow">{{inventory.soldprice}}</td>
       <td class="invmanrow">{{inventory.shipcost}}</td>
       {% if inventory.status == 'REFUNDED' %}
       <td>{{inventory.refund_loss}}</td>
       {% endif %}
       {% if inventory.status == 'Partial Refund' %}
       <td>-{{inventory.refund_amount}}</td>
       {% endif %}
       <td>{{inventory.status}}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
  <h2 class="text-center text-danger">Total Loss: {{total_refund_loss}}</h2>
  <p class="text-center">The above denotes the total amount of money lost from refunded items. Amount accounted for is the total paid, total sold and ship cost total for the product.</p>
  {% endif %}
</div>
</div>
{% endblock %}

models.py

class Inventory(models.Model):
    paid = models.DecimalField(default=0, max_digits=5, decimal_places=2)
    soldprice = models.DecimalField(default=0, max_digits=5, decimal_places=2, blank=True)
    shipcost = models.DecimalField(default=0, max_digits=5, decimal_places=2, blank=True)
    refund_amount = models.DecimalField(default=0, max_digits=5, decimal_places=2)

    @property
    def refund_loss(self):
            return 0 - self.paid - self.soldprice - self.shipcost

我怎样才能隐藏那个错误,这样如果没有数据,我就不会得到那个错误,只显示它应该显示的模板?任何帮助表示赞赏。

python django
© www.soinside.com 2019 - 2024. All rights reserved.