django checking many-to-many relationship for uniqueness

此生再无相见时 提交于 2019-12-11 12:41:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!