def clean(self):
data = super(ProductClassForm, self).clean()
has_variants = self.cleaned_data['has_variants']
product_attr = set(self.cleaned_data['product_attributes'])
variant_attr = set(self.cleaned_data['variant_attributes'])
if not has_variants and len(variant_attr) > 0:
msg = pgettext_lazy(
'Product class form error',
'Product variants are disabled.')
self.add_error('variant_attributes', msg)
if len(product_attr & variant_attr) > 0:
msg = pgettext_lazy(
'Product class form error',
'A single attribute can\'t belong to both a product '
'and its variant.')
self.add_error('variant_attributes', msg)
if self.instance.pk:
variants_changed = not (self.fields['has_variants'].initial ==
has_variants)
if variants_changed:
query = self.instance.products.all()
query = query.annotate(variants_counter=Count('variants'))
query = query.filter(variants_counter__gt=1)
if query.exists():
msg = pgettext_lazy(
'Product class form error',
'Some products of this type have more than '
'one variant.')
self.add_error('has_variants', msg)
return data
评论列表
文章目录