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.
* status: new => closed
* resolution: => invalid
Comment:
`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!
--
Ticket URL: <https://code.djangoproject.com/ticket/34057#comment:1>