django检查多对多关系的独特性 [英] django checking many-to-many relationship for uniqueness

查看:111
本文介绍了django检查多对多关系的独特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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()函数先前被调用要保存,但我想知道如何测试,看看业务之间的多对多关系是否发生变化。任何想法?

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?

编辑

该表单也会抛出错误由于业务m2m提交。在保存之前是否需要额外的处理?

The form throws up an error as well when submitted due to the business m2m. Does it need additional processing before saving?

推荐答案

业务应该是 business :)

这篇关于django检查多对多关系的独特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆