我重写了一个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