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

Add keys(), __contains__ method to InventreeObject #198

Closed miggland closed 11 months ago

miggland commented 11 months ago

Without these, calling code such as the following on R being any InventreeObject, will fail:

dict(R)
'pk' in R
R.keys()

with an error something like this:

  File "... inventree-python/inventree/base.py", line 310, in __getitem__
    raise KeyError(f"Key '{name}' does not exist in dataset")
KeyError: "Key '0' does not exist in dataset"

After adding these methods, the above results in something like this:

>>> dict(R)
{'pk': 12, 'name': 'Dimension', 'units': '', 'description': ''}
>>> R.keys()
dict_keys(['pk', 'name', 'units', 'description'])
>>> 'pk' in R
True
SchrodingersGat commented 11 months ago

Thanks @miggland nice addition