انظر منشورنا السابق InvenTree Python API: مثال اتصال API مبسّط باستخدام تهيئة YAML لطريقتنا في إنشاء كائن api باستخدام ملف تهيئة YAML!
يجد الكود التالي فئة قطع تسمى ICs وينشئ قطعة جديدة في تلك الفئة.
أولًا، نحتاج لجلب فئات القطع، هذا من منشورنا السابق InvenTree Python API: مثال اتصال API مبسّط باستخدام تهيئة YAML:
inventree_part_categories.py
from inventree.part import PartCategory
all_categories = PartCategory.list(api)
# Dict of part categories by name
# (e.g. 'OpAmps')
part_categories_by_name = {
category["name"]: category
for category in all_categories
}
# Dict of part categories by public key (e.g. 7)
part_categories_by_pk = {
category.pk: category
for category in all_categories
}
# Dict of part categories by hierarchical path
# (e.g. 'Electronics-Components/ICs/OpAmps')
part_categories_by_pathstring = {
category.pathstring: category
for category in all_categories
}الآن لنختر الفئة الصحيحة:
select_category.py
ics = part_categories_by_name['ICs']الآن حان أخيرًا وقت إنشاء القطعة:
inventree_create_part.py
from inventree.part import Part
new_part = Part.create(api, {
'name': 'L78L33ABD',
'description': '100mA 3.3V fixed LDO regulator, SOIC-8',
'category': ics.pk
})يمكنك قراءة المفتاح الأساسي (pk) للقطعة المولّدة حديثًا باستخدام
inventree_new_part_pk.py
new_part.pkIf this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow