#26786: Redundant default MaxValueValidator with PostgreSQL database backend
-------------------------------------+-------------------------------------
     Reporter:  e-stepanov           |      Owner:  nobody
         Type:  Bug                  |     Status:  new
    Component:  Database layer       |    Version:  1.9
  (models, ORM)                      |   Keywords:  IntegerField,
     Severity:  Normal               |  validators, MaxValueValidator
 Triage Stage:  Unreviewed           |  Has patch:  0
Easy pickings:  0                    |      UI/UX:  0
-------------------------------------+-------------------------------------
 For example, I have the following model:

 {{{
 #!python
 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:

 {{{
 #!python
 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
 
[https://docs.djangoproject.com/en/1.9/_modules/django/db/models/fields/#IntegerField
 source code for IntegerField] there is code for adding MaxValueValidator
 and MinValueValidator:

 {{{
 #!python
 @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.

--
Ticket URL: <https://code.djangoproject.com/ticket/26786>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.b472c53b99e45fdc3ac575805f75d675%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to