Hello guys.
I’m trying to manipulate data inside a table and I’m stuck with something.
I want to make the column “Total price” show the result of a multiplication between “Quantity” and “Unit price”.
I’m succesfully adding new rows using:
app_tables.bom.add_row (Equipment=“Pump”, Quantity=3,UnitPrice=10,TotalPrice=30)
But as you can see, I’m defining a number on TotalPrice to make it work.
What I want is something similar to:
app_tables.bom.add_row (Equipment=“Pump”, Quantity=3,UnitPrice=10,TotalPrice=(Quantity*UnitPrice))
Can anybody help me figure this out?
I cannot leave like this because the quantity may vary.
Are Quantity and Unit Price assigned to variables before adding the row? If so, you can use those to calculate the Total Price.
As @duncan_richards12 says, you’d probably have variables for quantity and unit price anyway, so use those to calculate total price, e.g.:
def add_bom_row(equipment, quantity, unit_price):
app_tables.bom.add_row (Equipment=equipment, Quantity=quantity,UnitPrice=unit_price,TotalPrice=quantity*unit_price))
Or, if you don’t want to store total price at all (a common technique since you can calculate it from other fields in the data table), just store quantity and unit price, and when you’re displaying the row in a data grid, add a column in the data grid and use data binding to calculate the total price, e.g.:
self.item['Quantity']*self.item['UnitPrice']
1 Like
A database is a tool to store information, it is not going to calculate anything for you.
You are already doing the right thing.
I wasn’t assigning them first. I was trying the harder way - accessing the specific row/column and trying to retrieve the number. Now I’ve assigned them and it works. This is way easier. Thanks!
1 Like