开发者

Intermediate model formset validation

开发者 https://www.devze.com 2023-02-10 18:09 出处:网络
I am wondering how to specify some constraints on intermediate model formset. I have 3 classes in model:

I am wondering how to specify some constraints on intermediate model formset. I have 3 classes in model: Attribute, Product and AttributeValuation, which is intermediate for Attribute and Product:

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)

On top of that, I have built AttributeValuationInline with AttributeFormset, and registered it to ProductAdmin:

class AttributeValuationInline(admin.TabularInline):
   开发者_如何学运维 model = AttributeValuation
    extra = 0
    formset = AttributeFormset

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

class AttributeFormset(BaseInlineFormSet):
    def clean(self):
        pass

My question is: how can I check in the clean method the contents of each inline row (form)? I've tried through each form of self.forms in Formset, but I could not access the specific fields of Attribute model (imagine that there are some, I don't want to obfuscate my question with irrelevant data)?

In my example, I would like to have maximum of one Attribute of each type per Product (so that no one puts two or more attributes with the same type associated with one Product).


self.forms[0].cleaned_data

wont work?


I went through

   for form in self.forms:
       form.instance

And it's ok. Why should cleaned_data be better?

0

精彩评论

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