#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 , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 9 years ago
Has patch: | set |
---|
comment:4 by , 9 years ago
Patch needs improvement: | set |
---|---|
Summary: | Redundant default MaxValueValidator with PostgreSQL database backend → Redundant range validators on integer fields |
comment:6 by , 8 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Note:
See TracTickets
for help on using tickets.
Pull request
Tests for new functionality pass with SQLite and PostgreSQL.