Opened 14 years ago

Closed 14 years ago

#13916 closed (wontfix)

DecimalField min_value does not enforce use of decimal.Decimal type

Reported by: bugs@… Owned by: nobody
Component: Forms Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I was attempting to validate a ModelForm DecimalField using the min_value attribute, like so:

amount = forms.DecimalField(min_value = 0.01)

However, min_value requires the use of a Decimal object, like so:

amount = forms.DecimalField(min_value = decimal.Decimal("0.01"))

This makes sense. However, the first example should raise an exception as it's not a valid type for comparison. It also allows other strange types, like:

amount = forms.DecimalField(min_value = "fail")

This may affect other field types as well, I have not tested.

Furthermore, the documentation should reflect this: http://docs.djangoproject.com/en/1.2/ref/forms/fields/#django.forms.DecimalField.min_value

Django version 1.2.1

Change History (2)

comment:1 by Gregor Müllegger, 14 years ago

Not checking the type may be some problem in some cases. But usually this is a good thing and very common in python. The reason is ducktyping (you might have your own python object that can be compared to a Decimal but is no Decimal).
You could pass the ducktype as min_value and it would work -- but would fail if a type check is performed.

So I'm a +1 for not having typechecks. To quote Russell Keith-Magee:

isinstance checks are generally an indication that you're doing
something wrong (or at least that you could be doing it better).

comment:2 by Matthias Kestenholz, 14 years ago

Resolution: wontfix
Status: newclosed

I think the developer can be expected to know the distinction between floats and decimals.

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