#34057 closed Bug (invalid)

Validation of DecimalField

Reported by: Shivan Sivakumaran Owned by: nobody
Component: Database layer (models, ORM) Version: 4.1
Severity: Normal Keywords: decimal, float
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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
class Lift(models.Model):

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

Here is my factory:

class LiftFactory(factory.DjagoModelFactory):
    def bodyweight(self):
        return round(random.uniform(50, 150), 2)

When I run my test, I get an error:

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:

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.

Change History (1)

comment:1 Changed 12 months ago by Tim Graham

Resolution: invalid
Status: newclosed

DecimalField is backed by Decimal, not float. so the problem is indeed with your code. For "is it a bug?" questions, please use the resources at TicketClosingReasons/UseSupportChannels rather than this ticket tracker. Thanks!

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