开发者

django checking many-to-many relationship for uniqueness

开发者 https://www.devze.com 2023-03-02 15:21 出处:网络
I have the following model.py: class FinancialProduct(models.Model): active = models.BooleanField(default=True)

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() functi开发者_运维问答on 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?


business should be businesses :)

0

精彩评论

暂无评论...
验证码 换一张
取 消