I am using Django 3.0 and Python 3.7
here is my views.py
@login_required
def updated_status(request, pk):
s1 = []
purchase_quantity = Item.objects.filter(order_id=pk)
data = set()
a = [x.id for x in purchase_quantity if x.id not in data and not data.add(x.id)]
stock_quantity = Stock.objects.filter(other_id=pk)
data1 = set()
b = [x.other_item.id for x in stock_quantity if
x.other_item.id not in data1 and not data1.add(x.other_item.id)]
if data == data1:
for j in data1:
stock_quantity = Stock.objects.filter(other_order_id=pk,
other_item_id=j).aggregate(Sum('quantity'))
purchase_quantity = Item.objects.filter(
other_id=pk, id=j).aggregate(Sum('quantity'))
if stock_quantity > purchase_quantity:
Order.objects.filter(id=pk).update(flag=1)
if stock_quantity >= purchase_quantity:
s1.append('true')
else:
s1.append('false')
if 'false' in s1:
Order.objects.filter(id=pk).update(status=3)
else:
Order.objects.filter(id=pk).update(status=2)
else:
pass
return redirect(reverse('get_list_data'))
How could i solve this issue " TypeError: '>' not supported between instances of 'dict' and 'dict' "
CodePudding user response:
try this
Django aggregate return dict object.
https://docs.djangoproject.com/en/4.0/topics/db/aggregation/
for j in data1:
stock_quantity = StockMovements.objects.filter(other_order_id=pk, other_order_item_id=j).aggregate(Sum('quantity'))
purchase_item_quantity = OtherOrderItem.objects.filter(other_order_id=pk, id=j).aggregate(Sum('quantity'))
if stock_quantity.get('quantity__sum') > purchase_item_quantity.get('quantity__sum'):
OtherOrder.objects.filter(id=pk).update(flag=1)
if stock_quantity.get('quantity__sum') >= purchase_item_quantity.get('quantity__sum'):
pass
aggregate() is a terminal clause for a QuerySet that, when invoked, returns a dictionary of name-value pairs. The name is an identifier for the aggregate value; the value is the computed aggregate. The name is automatically generated from the name of the field and the aggregate function. If you want to manually specify a name for the aggregate value, you can do so by providing that name when you specify the aggregate clause:
CodePudding user response:
Both stock_quantity
and purchase_quantity
are dictionaries here: you need to unwrap the item from the dictionary, so:
stock_quantity = StockMovements.objects.filter(
other_order_id=pk, other_order_item_id=j
).aggregate(Sum('quantity'))['quantity__sum']
purchase_item_quantity = OtherOrderItem.objects.filter(
other_order_id=pk, id=j
).aggregate(Sum('quantity'))['quantity__sum']