Consider the following models:
class Arena(models.Model): crowd_capacity = models.PositiveInteger()
admin.py:
class SectionInline(admin.StackedInline): model = Section fk_name = 'arena' extra = 1 class ArenaAdmin(admin.ModelAdmin): inlines = [ SectionInline, ]
I want to add a validation method to verify that the sum of all section.crowd_capacity does not exceed total arena.crowd_capacity.
At first I wanted to write a custom SectionFormSet method with a clean method, but then I did not see how to get arena.crowd_capacity.
I also tried to add a clean method to Arena, it really shows a red validation error, but does not fix this problem. It seems that the Arena cleanup method starts after all sections have been saved, and changing the section.crowd_capacity and we section has caused the problem to have no effect.
The verification method I tried:
def clean(self): super(Arena, self).clean() capacity = 0 for s in self.sections.all(): capacity += s.crowd_capacity if capacity > self.crowd_capacity: raise ValidationError('The sum of all sections crowd capacity ' 'exceeds arena crowd capacity')
Neara source share