我正在建立一个库存系统,我有一个页面,可以按类别查看销售历史记录,它将显示所有已售商品、已售数量、库存总量、剩余数量、销售额、利润等......随时我按类别搜索(例如文具),显示如下。
Item Name stock qty sold qty total stock total amount profit
Books 990 45 75 60 1035 900 990 14567 15666 12666 1777 1555 17777
Sciences 825 45 75 60 1035 900 990 14567 15666 12666 1777 1555 17777
Comics 930 45 75 60 1035 900 990 14567 15666 12666 1777 1555 17777
但我真正想要的是:
Item Name stock qty sold qty total stock total amount profit
Books 990 45 1035 14567 1777
Sciences 825 75 1035 15666 1555
Comics 930 60 990 12666 17777
从数据库中调用项目名称和库存数量并将其附加到列表中。对于已售商品的数量,我从数据库中调用此类商品的每个实例并计算它们的总数,然后附加到列表中。所以我想为他们各自的项目显示他们,数量和专业也是如此。我只想要如何操作模板以适当地显示它。我的视图代码如下:
def search_category (request):
collate=[]
gains=[]
amount=[]
name=[]
store=[]
qty=[]
if 'q' in request.GET and request.GET['q']:
q = request.GET.get('q','')
items = Item.objects.filter(category__name__exact=q)
for item in items:
collate.append(item.subcategory.subcategoryname)
qty.append(int(item.quantity))
len_collate=len(collate)
count=0
#for pick in collate:
keep1=[]
amt_save=[]
gain_save=[]
t=[]
all_amt_save=[]
#sum=0
#t_amount=0
#t_gain=0
while count < len_collate:
soldqty=[]
#histories = RecordSales.objects.filter(ItemName__subcategoryname__exact=pick)#here
histories = RecordSales.objects.filter(ItemName__subcategoryname__exact=collate[count])
lenght=len(histories)
for history in histories:
soldqty.append(history.quantity)
#keep1=[] #array to store sum
len_qty=len(soldqty)
#count=0
#while count < lenght:
sum=0
sums=[]
t_stock=[]
#for num in soldqty:
count_qty=0
#for num in soldqty:
while count_qty < len_qty:
sum=sum + soldqty[count_qty]
#t_stock=qty[count_qty] + sum
count_qty=count_qty + 1
amount.append(history.total)
t_amount=0
amt_count=0
while amt_count < len(amount):
t_amount=t_amount + amount[amt_count]
amt_count=amt_count + 1
gains.append(history.profit)
t_gain=0
gain_count=0
while gain_count < len(gains):
t_gain=t_gain + gains[gain_count]
gain_count=gain_count + 1
name.append(history.ItemName)
#keep1.append(sum)
len_keep1=len(keep1)
amt_save.append(t_amount)
gain_save.append(t_gain)
count=count + 1
keep1.append(sum)
all_amt_count=0
all_amt_total=0
while all_amt_count < len(amt_save):
all_amt_total= all_amt_total + amt_save[all_amt_count]
#all_amt_save.append(all_amt_total)
all_amt_count=all_amt_count + 1
all_g_count=0
all_g_total=0
while all_g_count < len(gain_save):
all_g_total=all_g_total + gain_save[all_g_count]
all_g_count=all_g_count + 1
t_count=0
while t_count < len(keep1):
t_stock=keep1[t_count] + qty[t_count]
t_count=t_count + 1
store.append(t_stock)
total_amount=0
keep_total=[]
for total in amount:
total_amount=total_amount + total
keep_total.append(total_amount)
#total_amount=0
return render_to_response('sell_category.html',
{'items':items,'query': q,'keep1':keep1,'name':name,'items':items,
'keep_total':keep_total,'t':store,'amt_save':amt_save,'gain_save':gain_save,'all_amt':all_amt_total,'t_gain':all_g_total,'len':len_keep1,
})
else:
return render_to_response('sell_category.html', {'error': True})
谢谢。