我有以下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,表单在提交时也会引发错误。保存之前是否需要额外处理?
答案 0 :(得分:1)
business
应为businesses
:)