#34057: Validation of DecimalField
-------------------------------------+-------------------------------------
               Reporter:  shivan-s   |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  decimal, float
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I'm using factory boy to create fixtures for my tests. I have this model:

 I am using:
 - django 4.1.1
 - factoryboy 3.2.1
 - pytest 7.1.3
 - pytest-django 4.5.2
 - pytest-factoryboy 2.5.0

 {{{#!python
 class Lift(models.Model):
     ...

     bodyweight = models.DecimalField(
         blank=True, max_digits=5, decimal_places=2
     )
 }}}

 Here is my factory:

 {{{#!python
 class LiftFactory(factory.DjagoModelFactory):
     ...

     @factory.lazy_attribute
     def bodyweight(self):
         return round(random.uniform(50, 150), 2)
 }}}

 When I run my test, I get an error:

 {{{#!python
 django.core.exceptions.ValidationError: {'bodyweight': ['Ensure that there
 are no more than 2 decimal places.']}
 }}}

 However, when I wrap the `random.uniform` with `Decimal`, it works!! Like
 so:
 {{{#!python
 return round(Decimal(random.uniform(50, 150), 2))
 }}}

 Is this a problem with how float and decimal types are dealt with? Or am I
 doing something wrong?

 I apologise, I don't know if this is a "mistake" with `factoryboy` or
 `django`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34057>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070183814a6101-db64e8b1-65d4-477d-880c-b6d040979fd2-000000%40eu-central-1.amazonses.com.

Reply via email to