2016-06-11 100 views
0

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

'库存=超(AddOrChangeProductsInventoryForm,个体经营).save(提交)'应该不会是'.save(提交=提交)'? – dtgq

+0

由于它是唯一的参数,是不是暗示它的位置? – Pepedou

回答

0

如果模型有任何许多一对多的关系,那么你需要确保在组合使用self.save_m2m()与保存方法。下面是一个简单的例子:

# save method of your forms.ModelForm class. 
def save(self): 
    obj = super(MyModelForm, self).save(commit=False) 
    obj.save() 
    self.save_m2m() 
    return obj 
+0

我试过你发布的东西无济于事。也许我正在更新相关对象错误? – Pepedou

+0

@Pepedou我会尝试从你的save方法中删除'commit = True'参数,而是在你调用'super'的行上指定'commit = False'作为参数。同时删除语句'如果不提交',并让每次都执行该代码。如果这些更改不起作用,那么它必须是'_update_inventory'方法中的某些内容。 – denvaar

+0

@Ppedpedou在你为产品和库存分配产品后,你也需要做一个'new_item.save()'。 – denvaar

0

当commit为False时,您正在保存表单。 替换(第10行):

if not commit: 

与:

if commit: 

UPDATE:

new_iteminventory.productinventoryitem_set.filter(product__sku=product_sku).first()

ProductInventoryItem() 
新副本

这是本地变量,它不更新inventory。执行该功能后,所有这些更改都将被破坏。您需要将更改存储在inventory中。

for obj in self.instance.productinventoryitem_set.all(): 
    obj.quantity += 2 
    obj.save() 

然后你保存更改前销毁:

当你在重写_save_m2m

你可以做这样的事情:

def save(self, commit=True): 

    inventory = super(AddOrChangeProductsInventoryForm, self).save(commit) 

    self._update_inventory_items_quantities(inventory, commit) 

    return inventory 

def _update_inventory_items_quantities(self, inventory, commit): 

    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 
     if commit: 
      new_item.save() 
+0

我已经完全删除了这个条件,但仍然没有改变。 – Pepedou

相关问题