Positive Validation Check Django Not Working

Viewed 32

I have to create this validation rule when Start= (start_new + start_old) >0 or is positive and End = (end_new + end_old) >0 or is positive then the validation error will raise that" Positive Strat and Positive End are not allowed in the conjunction",

But with my below code it is not checking the validation rule and allowing the positive start and positive end values in the conjunction.

my code in django form.py

        for i in range(count):
            start_new = int(self.data.get(f'applicationruntime_set-{i}-start_new') or 0)
            start_old = int(self.data.get(f'applicationruntime_set-{i}-start_old') or 0)
            end_new = int(self.data.get(f'applicationruntime_set-{i}-end_new') or 0)
            end_old = int(self.data.get(f'applicationruntime_set-{i}-end_old') or 0)
            if (start_new + start_old) > 0 and (end_new+end_old) > 0:
                raise ValidationError(
                    f" Positive Start  values and Positive End  values are not allowed to be used in conjunction")

1 Answers

Try to change priority:

((start_new + start_old) > 0) and ((end_new+end_old) > 0) 

but this way is better:

(start_new > start_old) and (end_new > end_old)

and, of course, OOP is much better:

if not self.date_range_is_valid():
    ...

and somewhere you should define the method date_range_is_valid. It help you to have validation method and change it for business asks.

Related