2
0
mirror of https://github.com/inventree/inventree-docs.git synced 2025-04-27 21:26:43 +00:00

Add example about updating fields via the python interface (#355)

This commit is contained in:
Oliver 2022-08-12 12:01:58 +10:00 committed by GitHub
parent fe23bfdb9a
commit 433e577f33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -144,6 +144,37 @@ couch = Part.create(api, {
})
```
#### Updating Attributes
Most model fields which are exposed via the API can be directly edited using the python interface, by simply calling the `save()` method as shown below:
```python
from inventree.api import InvenTreeAPI
from inventree.part import Part
api = InvenTreeAPI(host='http://localhost:8000', username='admin', password='inventree')
# Retrieve part instance with primary-key of 1
part = Part(api, pk=1)
# Update specified part parameters
part.save(data={
"description": "New part description",
"minimum_stock": 250,
})
# Reload data from remote server
part.reload()
# Display updated data
print("Part Name:", part.name)
print("Description:", part.description)
print("Minimum stock:", part.minimum_stock)
```
!!! info "Read Only Fields"
Note that some fields are read-only and cannot be edited via the API
#### Adding Parameters
Each [part](../part/part.md) can have multiple [parameters](../part/parameter.md). For the example of the sofa (above) *length* and *weight* make sense. Each parameter has a parameter template that combines the parameter name with a unit. So we first have to create the parameter templates and afterwards add the parameter values to the sofa.