Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26786 closed Bug (fixed)

Redundant range validators on integer fields

Reported by: Eduard Stepanov Owned by: Eduard Stepanov
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords: IntegerField, validators, MaxValueValidator
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

For example, I have the following model:

from django.db import models
from django.core.validators import MaxValueValidator

class AModel(models.Model):
    number = models.IntegerField("Test", validators=[MaxValueValidator(100)])

and the following form:

class AForm(forms.ModelForm):
    class Meta:
        model = AModel
        fields = "__all__"

I use PostgreSQL as database backend. In this case, when AForm instance is created with number's field value more than 2147483647, two error messages are created:

>>> f = AForm({'number': 1111111111111})
>>> f.is_valid()
False
>>> f.errors
{'number': [u'Ensure this value is less than or equal to 100.', u'Ensure this value is less than or equal to 2147483647.']}

In the source code for IntegerField there is code for adding MaxValueValidator and MinValueValidator:

@cached_property
    def validators(self):
        # These validators can't be added at field initialization time since
        # they're based on values retrieved from `connection`.
        range_validators = []
        internal_type = self.get_internal_type()
        min_value, max_value = connection.ops.integer_field_range(internal_type)
        if min_value is not None:
            range_validators.append(validators.MinValueValidator(min_value))
        if max_value is not None:
            range_validators.append(validators.MaxValueValidator(max_value))
        return super(IntegerField, self).validators + range_validators

So as result two MaxValueValidators and two error messages are created. I think this code should check for another similar validators before adding these ones.

Change History (8)

comment:1 by Claude Paroz, 8 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Eduard Stepanov, 8 years ago

Owner: changed from nobody to Eduard Stepanov
Status: newassigned

comment:3 by Eduard Stepanov, 8 years ago

Has patch: set

Pull request

Tests for new functionality pass with SQLite and PostgreSQL.

comment:4 by Simon Charette, 8 years ago

Patch needs improvement: set
Summary: Redundant default MaxValueValidator with PostgreSQL database backendRedundant range validators on integer fields

comment:5 by Eduard Stepanov, 8 years ago

Patch needs improvement: unset

Patch has been updated.

comment:6 by Tim Graham, 8 years ago

Triage Stage: AcceptedReady for checkin

comment:7 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 49b4596:

Fixed #26786 -- Avoided redundant max value validators on integer fields.

comment:8 by Tim Graham <timograham@…>, 8 years ago

In 28de25c2:

[1.10.x] Fixed #26786 -- Avoided redundant max value validators on integer fields.

Backport of 49b4596cb4744e4b68d56e6a540a3e15c1582963 from master

Note: See TracTickets for help on using tickets.
Back to Top