我正在尝试使用 pk 过滤对象后对完整字段求和。
Views.py
def items(request, pk):
current_user = request.user
selected_itemz = get_object_or_404(ItemIn, pk=pk)
all_cats = Category.objects.all()
cat_count = all_cats.count()
item_count = ItemIn.objects.values_list('item_name', flat=True).distinct().count() # returns a list of tuples..
#all_units = Item.objects.aggregate(Sum('item_quantity'))['item_quantity__sum']
ItemOut_table = ItemOut.objects.all().filter(item_name=selected_itemz)
ItemOut_quantity = ItemOut_table.aggregate(Sum('item_quantity'))['item_quantity__sum']
context = {
#'all_units': all_units,
'item_count': item_count,
'cat_count': cat_count,
'current_user': current_user,
'ItemOut_quantity': ItemOut_quantity,
'selected_itemz':selected_itemz,
}
return render(request, 'townoftech_warehouse/item_details.html', context)
然后我使用了我创建的额外过滤器subtract
在我的 HTML 中
HTML
<br>
<br>
<p align="right">
الكمية الموجودة:
{{ selected_itemz.item_quantity|subtract:ItemOut_quantity }}
</p>
<br>
<br>
这是tempaltetags
file
from django import template
register = template.Library()
@register.filter
def subtract(value, arg):
return value - arg
现在我收到错误:
TypeError at /item/1/
unsupported operand type(s) for -: 'int' and 'NoneType'