我重寫了一個ModelForm的保存方法。我解析一個Excel文件,如果有一些值,我更新窗體的實例相關對象中的數量。它是第一次,當實例沒有相關的對象。但是第二次,當我更新這些值時,沒有任何反應。我不確定它是否與commit
參數有關。調用save()後Django模型沒有被保存?
編輯 - 相關代碼:
def save(self, commit=True):
"""
Overrides the save method in order to add the product inventory items listed in
the uploaded Excel file, if one was uploaded.
"""
inventory = super(AddOrChangeProductsInventoryForm, self).save(commit)
self._update_inventory_items_quantities()
if not commit:
inventory.save()
self.save_m2m()
return inventory
def _update_inventory_items_quantities(self):
inventory = self.instance
if len(self.excel_data_dict) == 0:
return inventory
non_existing_products = []
for i, product_sku in enumerate(self.excel_data_dict['SKU']):
quantity = self.excel_data_dict['Cantidad'][i]
new_item = inventory.productinventoryitem_set.filter(product__sku=product_sku).first()
if new_item is None:
product = Product.objects.filter(sku=product_sku).first()
if product is None:
# TODO: Raise warning in view
non_existing_products.append(product_sku)
continue
new_item = ProductInventoryItem()
new_item.product = product
new_item.inventory = inventory
new_item.quantity += quantity
# TODO: Check why after first update it's not being updated
'庫存=超(AddOrChangeProductsInventoryForm,個體經營).save(提交)'應該不會是'.save(提交=提交)'? – dtgq
由於它是唯一的參數,是不是暗示它的位置? – Pepedou