2014-02-05 29 views
0

我試圖保存客戶記錄的現有實例。其車型擁有M2M車型(因爲客戶可以有多輛車)。在閱讀了幾個問題/答案後,我仍然不知道如何解決這個問題。更新Django中現有的M2M關係

顧客模型:

class Customer(models.Model): 
    vehicle_id = models.ManyToManyField(VehicleSale) 
    name = models.CharField(max_length=40, blank=True, db_index=True, null=True, 
            verbose_name='name') 
    lic = models.CharField(max_length=20, blank=True, db_index=True, null=True, 
           verbose_name='license') 
    addr = models.CharField(max_length=40, blank=True, null=True, verbose_name='address') 
    city = models.CharField(max_length=15, blank=True, null=True, verbose_name='city') 
    state = models.CharField(max_length=2, blank=True, null=True, verbose_name='state') 
    zip = models.CharField(max_length=10, blank=True, null=True, verbose_name='zipcode') 
    email = models.EmailField(blank=True, null=True, verbose_name='email') 
    tel1 = models.CharField(max_length=15, blank=True, verbose_name='Tel. 1', null=True) 
    tel2 = models.CharField(max_length=15, blank=True, verbose_name='Tel. 2', null=True) 
    ssn = models.CharField(max_length=12, blank=True, db_index=True, null=True,verbose_name='SSN') 

    class Meta: 
     db_table = 'customer' 

    def __unicode__(self): 
     return self.name 

    def save(self, *args, **kwargs): 
     self.name = self.name.upper() 
     self.addr = self.addr.upper() 
     self.city = self.city.upper() 
     self.state = self.state.upper() 

     return super(Customer, self).save(*args, **kwargs) 

在視圖中,定義客戶爲

customer = current_vehicle.customer_set.all() 

後我嘗試以下:

if 'customer' in request.POST: 
    if customer: 
     customer_form = CustomerForm(request.POST, instance=customer[0]) 
     if customer_form.is_valid(): 
      customer_form.save() 

還試圖customer_form之前加入定義:

customer.vehicle_id = current_vehicle.id 

和表單後則此:

customer_form.vehicle_id = current_vehicle.id 

形式是無效的,所以它不會被保存。在檢查{{form.errors}}時,它總是報告vehicle_id是必需的。

最後,在this答案後,我把它調整到我的方案中加入:

obj = customer_form.save(commit=False) 

,並希望指定vehicle_id,但立即失敗。

我錯過了什麼?

謝謝。

月1日編輯: 視圖上的部分現在看起來:

customer_form = CustomerForm(request.POST, instance=customer[0]) 
customer_form.save() 
customer_form.vehicle_id.add(current_vehicle) 
+0

車輛是否可以與多個客戶關聯?如果不是,那麼我認爲你的車型不正確,你應該在車型上有一個客戶外鍵,並且根本沒有M2M字段。請顯示模型定義並顯示您的完整視圖。 –

+0

你在什麼版本的Django上? – Anentropic

+0

是在原始源代碼中正確縮進的保存方法的最後一行嗎? – Anentropic

回答

1

你誤會一個多對多字段是什麼在這裏:

customer_form.vehicle_id = current_vehicle.id 

vehicle_id被定義爲一個多對多字段中輸入您客戶模型,因此你不能只分配一個單一的ID。你必須添加VehicleSale模型的實例,例如:

customer_form.vehicle_id.add(current_vehicle) 

在這裏看到的文檔:
https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/

另見這個答案你爲什麼不能保存,直到填充vehicle_id關係:
https://stackoverflow.com/a/2529875/202168

+0

好的,在完成customer_form = CustomerForm(request.POST,instance = customer [0]),添加customer_form.save()和THEN customer_form.vehicle_id.add(current_vehicle)後,嚴格遵循您提供的django文檔鏈接。不幸的是,在customer_form.save()行失敗,並出現一個值錯誤(「客戶無法更改,因爲數據未驗證」)。然而,request.POST確實有我做的改變,它只是在其中一個字段的末尾添加一個字母。順便說一句,這仍然有足夠的空間用於額外的角色。我究竟做錯了什麼? – Rmartin

+0

請顯示您的「客戶」模型的完整源代碼。當你在模型上調用'save()'時,它在模型上調用'full_clean',所以它在模型驗證中失敗 – Anentropic

+0

FUll客戶模型被添加。謝謝。 – Rmartin