inventree / inventree-python

Python library for communication with inventree via API
https://docs.inventree.org/en/latest/api/python/python/
MIT License
26 stars 34 forks source link

How to get a part #149

Closed lucassimon closed 1 year ago

lucassimon commented 1 year ago

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?


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)
        for part in parts:
            print(part)

if __name__ == '__main__':

    task = Parts()
    sotck_items = task.run()
SchrodingersGat commented 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>
lucassimon commented 1 year ago

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)
miggland commented 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)
print(p.in_stock)

Output:

1957.0
matmair commented 1 year ago

@miggland thank for your help!

lucassimon commented 1 year ago

What's the difference between the fields in stock and unallocated stock?

matmair commented 1 year ago

@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

SchrodingersGat commented 1 year ago

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.

SchrodingersGat commented 1 year ago

https://docs.inventree.org/en/latest/build/allocate/

lucassimon commented 1 year ago

thank you guys for your support