[Answer]-Model validator not validating before ModelForm clean

1👍

✅

You can use the model field’s clean, rather than validating before clean is executed.
Something like this:

from django.core.exceptions import ValidationError

class ResourceCapacityChangeForm(forms.ModelForm):

    class Meta:
        model = CapacityOnDemand

    def clean_capacity_on_demand_code(self):
        cod_code = self.cleaned_data.get('capacity_on_demand_code')

        if not is_code_in_sequence(cod_code):
            raise ValidationError("This code is not in sequence. Unable to add code.")
        return cod_code

Leave a comment