问题
I have the following 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)
And the following 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
The is_unique() function is called prior to saving but I am wondering how I can test to see if the many-to-many relationship has changed for business. Any ideas?
EDIT
The form throws up an error as well when submitted due to the business m2m. Does it need additional processing before saving?
回答1:
business should be businesses :)
来源:https://stackoverflow.com/questions/5885163/django-checking-many-to-many-relationship-for-uniqueness