[Django] #34057: Validation of DecimalField

1 view
Skip to first unread message

Django

unread,
Sep 27, 2022, 7:31:22 PM9/27/22
to django-...@googlegroups.com
#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.

Django

unread,
Sep 27, 2022, 10:06:18 PM9/27/22
to django-...@googlegroups.com
#34057: Validation of DecimalField
-------------------------------------+-------------------------------------
Reporter: Shivan Sivakumaran | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: decimal, float | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* 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>

Reply all
Reply to author
Forward
0 new messages