This is somewhat related to my previous question about creating an inventory but now I’m having trouble adding items to said inventory via server function. I have read a few things on the web about adding to a python list but I not for sure the same method works with anvil tables
Right now all I have is getting the data I need to add to:
You should probably try the straightforward approach first. Your target variable is a list of dictionaries, loop through it to find the right item and set the amount in it. Then see if that change is reflected in the data table.
now that I got back the object how could I turn this
{"items":[{"name":"Cookie","amount":"1"}]}
into this
{"items":[{"name":"Cookie","amount":"1"}{"name":"Bag Of Food","amount":"2"}]}
would I use something like
@anvil.server.callable
def add_item(item,amount):
user = anvil.users.get_user(allow_remembered=True)
row = app_tables.inventories.get(Id=user['email'])
for x in row['Inventory']['items']:
x.append()
Another question how would I remove items from a simple object therefore I could also allow the selling of inventory items, also adding to the quantity if you buy more of a matching item
The overall concept, and why @stucork’s solution works, is that you are able to go to a place where the object is stored (The ‘cell’ in a datatable row ) , and then retrieve it for whatever purposes you wish.
If you wish for the object in the data table row to be different in any way, you operate on the item your retrieved, and then you have to return it to the data table row. You are not operating on the object, you are operating on a copy that you have to “write back” to the data table.
If you are using this for inventory or purchasing you now also have to consider what will happen if multiple consumers of the data table access (human or machine) attempt to modify the same object at the same time, since they all operate on their own temporally independent copy and return their own result.
This is why your question is more complicated than it may seem at first glance, but anvil has a solution for that, you just have to make design decisions and continue building with those design decisions in mind without mixing different types of CRUD and or ACID logic. (Links to Wikipedia)
The solution anvil has is called Transactions and they are very easy to implement.
However, as someone who has worked on company sites with day in and day out live sales and close to a million different SKU’s in live inventory I will tell you a data object is probably not the best solution to use.
You should probably have an independent table with product ID’s in integer form, along with product names as text and inventory levels as integers, and link to that table from other tables that wish to have access to those inventory levels. You can even do without the product names if you store that information in a different linked place, using the same ID’s.
Also logic should be in place when modifying to stop overselling etc. (Selling past zero quantity)
I forgot to mention I’m working with a virtual inventory system kinda like that of a game in which a users inventory is stored in a simple object column in a data table, but I will look into transactions and overselling logic
When you execute that line you get a standard Python dictionary back out. You can do whatever you want with that Python dictionary, including finding a list inside it and appending a new item to it. You’re not doing anything Anvil specific at that point, just plain Python.
When you’re done with your manipulations to the dictionary:
that line updates the data table with the new version of the dictionary.