On Monday, April 15, 2019 at 10:13:30 PM UTC-7, mostwanted wrote: > > I have a database of items sold, I am updating the quantity of an item by > subtracting the amount purchased from the current available Quantity. How > do i replace the old value with the difference between the current value > and the purchased value? > > In the controller below i have an *itemCount *for loop (highlighted in > red) that attempts to capture the selected item and its quantity, draw from > the database the similar item and subtract its specified quantity > (session.value) from the database Quantity value then update the database > Quantity column withe new value which is the difference between Quantity > and session.value > > > CONTROLLER > > > def buy(): > if not session.cart: > session.flash = 'Add something to shopping cart' > redirect(URL('index')) > invoice = session.invoiceNo > total = sum(db.product(id).price*qty for id,qty in session.cart.items()) > for key, value in session.cart.items(): > db.sales.insert(invoice=invoice,buyer=auth.user.id,product = > key,quantity = value,price=db.product(session.key).price) > > > > > *itemCount=db(db.product.name==key).select(db.product.ALL) for item in > itemCount: quantity=item.Quantity-value > item.update_record(Quantity=quantity)* > > session.cart.clear() > session.flash = 'Sale Complete' > redirect(URL('invoice',args=invoice)) > return dict(cart=session.cart,form=form,total=total) > > > I am hoping that the itemCount for loop will do the calculations and > update the Quantity column with the new value when the sale is being made. > I expect to find a new Quantity value when i look at the purchased items in > the database table, but that is not happening, how cn i fix this? > > > Mostwanted >
I suspect that you're getting None in itemCount ... in the line above the for loop, the list comprehension is getting id, qty from session.cart.items(), but you're expecting key from key, value in session.cart.items() to match db.product.name. I don't see that one iterator will be returning two different lists. (P.S. I mess up intermediate values a lot, so I use a lot of print statements, which works ok when I have a console (like at home), or when the Scheduler captures my task's stdout, but I also know how to turn on logging (it's simple enough even for me!) so I can do it right in production.) /dps -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.