2011-05-04 25 views
1

關係,我有以下model.pyDjango的檢查很多一對多的唯一性

class FinancialProduct(models.Model): 
    active = models.BooleanField(default=True) 
    name = models.CharField(max_length=20) 
    abbreviation = models.CharField(max_length=3) 
    table_name = models.CharField(max_length=5, choices=FP_TABLE_CHOICES) 
    businesses = models.ManyToManyField(Business) 

而下面forms.py

class FinancialProductForm(ModelForm): 
    business = ModelMultipleChoiceField(widget=CheckboxSelectMultiple(),required=True) 

    class Meta: 
     model = FinancialProduct 

    def is_unique(self,latestFP): 
     if (self.cleaned_data['active'] == latestFP.active and 
      self.cleaned_data['name'].capitalize() == latestFP.name and 
      self.cleaned_data['acronym'].upper() == latestFP.acronym and 
      self.cleaned_data['table_name'] == latestFP.Table_name and 
      self.cleaned_data['business'] == latestFP.business): 
      return False 
     else: 
      return True 

is_unique()功能保存之前調用,但我想知道我如何測試以查看business的多對多關係是否已更改。有任何想法嗎?

編輯

時,由於業務M2M提交的形式拋出了一個錯誤,以及。在保存之前是否需要額外的處理?

+1

你所說的「多對一對多的關係已經改變爲經營」的意思是? – 2011-05-05 08:42:27

+0

你的權利。我在說什麼?!?它應該是'企業'(ARRRR !!!錯別字)。如果你把它作爲答案,我會打個勾。 – Sevenearths 2011-05-05 13:33:10

+0

好的,謝謝:) – 2011-05-05 13:52:21

回答

1

business應該businesses :)