Closed lucassimon closed 1 year ago
from inventree.api import InvenTreeAPI
from inventree.part import Part
api = InvenTreeAPI("https://demo.inventree.org", username="admin", password="inventree")
p = Part(api, pk=8)
for item in p.getStockItems():
print(item)
returns
<class 'inventree.stock.StockItem'><pk=860>
<class 'inventree.stock.StockItem'><pk=862>
<class 'inventree.stock.StockItem'><pk=859>
<class 'inventree.stock.StockItem'><pk=861>
<class 'inventree.stock.StockItem'><pk=299>
In this way I need to interable in this for loop and count each stock item. But I was thinking more like this:
from inventree import api
from inventree.part import Part
class Parts:
def __init__(self) -> None:
self.api = api.InvenTreeAPI('http://0.0.0.0:8000', connect=False)
self.api.token = 'foo'
self.api.use_token_auth = True
self.api.connect()
def run(self):
# parts = Part(api=self.api, pk=8).getStockItems()
# parts = Part.list(self.api, part=8)
parts = Part.get(self.api, pk=8)
print(parts.in_stock)
if __name__ == '__main__':
task = Parts()
sotck_items = task.run()
And in the base.py
class InventreeObject(object):
""" Base class for an InvenTree object """
@classmethod
def get(cls, api, pk, **kwargs):
""" Return a list of all items in this class on the database.
Requires:
URL - Base URL
"""
# Check if custom URL is present in request arguments
if 'url' in kwargs:
url = kwargs.pop('url')
else:
url = cls.URL
params = kwargs
url = url + f"/{pk}/"
response = api.get(url=url, params=params, **kwargs)
if response is None:
return None
if isinstance(response, dict) and response is not None:
return cls(data=response, api=api)
from inventree.api import InvenTreeAPI
from inventree.part import Part
api = InvenTreeAPI("https://demo.inventree.org", username="admin", password="inventree")
p = Part(api, pk=8)
print(p.in_stock)
Output:
1957.0
@miggland thank for your help!
What's the difference between the fields in stock
and unallocated stock
?
@lucassimon in_stock is the number of stock items that are available; unallocated is the number of stock items that are not allocated - maybe read https://docs.inventree.org/en/latest/build/allocate/ to get an overview how allocations work
in stock
is the total quantity of that particular part. unallocated
means the "available" amount - e.g. you may have "allocated" some stock to a build order or a sales order.
thank you guys for your support
Hello, I am trying to run the code below to get a single part and their stock number. But unsuccessful. I receive a list of parts. How can I do it?