2

I've created a custom validator which checks, if the upper number in RangeField is lower than some number.

The problem is that I can see in debug mode that the Validator.compare returns False but Django raises DataError

Value "21474834555" is out of range for type integer LINE 1: ...NULL, "cena" = NULL, "poznamka" = '', "rozloha" = '[ -214748...

I can't figure out why it raises this error when it should raise ValidationError before this.

I know that the number is out of range, that's why I've created the Validator.

VIEW

class DopytUpdateView(LoginRequiredMaklerAccessMixin, UpdateView):
    model = Dopyt
    form_class = DopytForm
    template_name = 'dopyty/dopyt.html'

FORM

class DopytForm(forms.ModelForm):

    class Meta:
        model = Dopyt
        fields = [...'rozloha',...]

VALIDATOR

class RangeCompleteMaxValueValidator(MaxValueValidator):
    def compare(self, a, b):
        upper_ok = (a.upper > b) if a.upper else True
        lower_ok = (a.lower > b) if a.lower else True
        return upper_ok and lower_ok

MODEL

class Dopyt(TimeStampedModel):
    ...
    rozloha = IntegerRangeField(null=True, blank=True, verbose_name='Rozloha [m2]',
                                validators=[RangeCompleteMinValueValidator(-2147483648),
                                            RangeCompleteMaxValueValidator(2147483647)])

Do you know why it behaves this way?

EDIT

As you can see, validator returns False

enter image description here

Milano
  • 18,048
  • 37
  • 153
  • 353
  • 1
    `a.upper` should probably be `a.upper()`? – Willem Van Onsem Jul 09 '18 at 10:44
  • @WillemVanOnsem No, it shouldn't, I've checked it. Moreover, I'm sure that validator returns False, check the bottom of the question, I've added a Pycharm Debug screenshot. – Milano Jul 09 '18 at 10:47
  • Aah... `a` and `b` aren't strings, these are ranges? – Willem Van Onsem Jul 09 '18 at 10:48
  • Yes, check the model: rozloha = IntegerRangeField(null=True, blank=True, verbose_name='Rozloha [m2]', validators=[RangeCompleteMinValueValidator(-2147483648), RangeCompleteMaxValueValidator(2147483647)]) – Milano Jul 09 '18 at 10:49

1 Answers1

0

Form validator cant see models validators, you must use clean if you want to avoid to reach model layer

You validador runs at your models instance, so when you going to save... while form.is_valid runs while in form, so this validations dont reach your models... to validate your logic at your form you should use clean

from django import forms

class ContactForm(forms.Form):
    # Everything as before.
    ...

    def clean(self):
        cleaned_data = super().clean()
        cc_myself = cleaned_data.get("cc_myself")
        subject = cleaned_data.get("subject")

        if cc_myself and subject:
            # Only do something if both fields are valid so far.
            if "help" not in subject:
                raise forms.ValidationError(
                    "Did not send for 'help' in the subject despite "
                    "CC'ing yourself."
                )

https://docs.djangoproject.com/en/2.0/ref/forms/validation/

Diego Vinícius
  • 2,125
  • 1
  • 12
  • 23