django检查多对多关系的独特性

时间:2011-05-04 14:37:42

标签: django django-forms many-to-many

我有以下model.py

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 个答案:

答案 0 :(得分:1)

business应为businesses:)