In my model the validation is not validating for the boolean field, only one time product_field need to be checked , if two time checked raise
product_field = models.BooleanField(default=False)
product_field_count = 0
for row in range(0,product_field_count):
if self.data.getlist(f'product_info_set-{row}-product_field'):
product_field_count = 1
if product_field_count <= 1:
raise ValidationError(
_(
" Need to Be Select Once"
))
validation error.
CodePudding user response:
You can tidy up the check a little with sum(iterable, /, start=0)
:
product_field_name_count = sum(
[
True
for row in range(0, product_field_count)
if self.data.getlist(f"product_info_set-{row}-product_field") is not None
]
)
if product_field_name_count > 1:
raise ValidationError(
'Only one item of this type may be selected'
)
If you're finding this isn't working as expected, you may want to add print()
to assure underlying assumptions about where certain variables are correct.
import pprint
pprint.pprint(self)
pprint.pprint(self.__dict__)
pprint.pprint(self.data)