2016-06-11 51 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

相關問題