Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 中间模型表单集验证_Django_Django Models_Django Forms - Fatal编程技术网

Django 中间模型表单集验证

Django 中间模型表单集验证,django,django-models,django-forms,Django,Django Models,Django Forms,我想知道如何在中间模型表单集上指定一些约束。 我在模型中有3个班: 属性、产品和属性评估,属性和产品的中介: class Attribute(models.Model): type = models.CharField(max_length = 200) pass class Product(models.Model): attribute_values = models.ManyToManyField(Attribute, through='AttributeValu

我想知道如何在中间模型表单集上指定一些约束。 我在模型中有3个班: 属性、产品和属性评估,属性和产品的中介:

class Attribute(models.Model):
    type = models.CharField(max_length = 200)
    pass

class Product(models.Model):
    attribute_values = models.ManyToManyField(Attribute, through='AttributeValuation')

class AttributeValuation(models.Model):
    attribute = models.ForeignKey(Attribute)
    product = models.ForeignKey(Product)
除此之外,我还使用AttributeFormset构建了AttributeEvaluationOnline,并将其注册到ProductAdmin:

class AttributeValuationInline(admin.TabularInline):
    model = AttributeValuation
    extra = 0
    formset = AttributeFormset

class ProductAdmin(admin.ModelAdmin):
    inlines = (AttributeValuationInline,)

class AttributeFormset(BaseInlineFormSet):
    def clean(self):
        pass
我的问题是:如何在clean方法中检查每个内联行表单的内容?我已经尝试了Formset中self.forms的每一种形式,但是我无法访问属性模型的特定字段。假设存在一些字段,我不想用不相关的数据混淆我的问题

在我的示例中,我希望每个产品的每个类型最多有一个属性,这样就不会有人将两个或多个相同类型的属性与一个产品关联

self.forms[0].cleaned_data
不行吗?

我通过了

   for form in self.forms:
       form.instance
没关系。为什么数据应该更好