Django

Code

Ticket #5355 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

DecimalField may "clean" itself and then raise an Exception

Reported by: Weipin Xia <webbing@gmail.com> Assigned to: PhiR
Milestone: Component: Forms
Version: SVN Keywords: newforms, sprintsept14
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

DecimalField? may "clean" itself and then raise an Exception. In my case, it happens when I put a DecimalField? in a django.newforms.Form class which will be used to "Edit" the model.

I suppose it would be necessary to check the type of parameter value like some of the other "Field" classes, such as DateField?.

         super(DecimalField, self).clean(value)
         if not self.required and value in EMPTY_VALUES:
             return None
         value = value.strip() # raise AttributeError, 'Decimal' object has no attribute 'strip'
         try:
             value = Decimal(value)

Attachments

fields.py.patch (441 bytes) - added by Weipin Xia <webbing@gmail.com> on 09/07/07 07:45:38.
5355.diff (1.3 kB) - added by PhiR on 09/14/07 16:09:04.
correct patch with regression tests

Change History

09/07/07 07:45:38 changed by Weipin Xia <webbing@gmail.com>

  • attachment fields.py.patch added.

09/14/07 15:45:01 changed by PhiR

  • needs_better_patch changed.
  • needs_tests changed.
  • keywords set to newforms.
  • needs_docs changed.
  • has_patch deleted.
  • stage changed from Unreviewed to Design decision needed.

There's no reason value would already be a decimal unless the data was a decimal in the first place. If you use a form with string input this will never happen. Maybe we should check in form inputs that the values are really strings and/or enforce this ?

09/14/07 16:04:47 changed by PhiR

  • keywords changed from newforms to newforms, sprintsept14.
  • owner changed from nobody to PhiR.
  • stage changed from Design decision needed to Accepted.

09/14/07 16:09:04 changed by PhiR

  • attachment 5355.diff added.

correct patch with regression tests

09/14/07 16:09:34 changed by PhiR

  • status changed from new to assigned.
  • has_patch set to 1.

09/15/07 05:44:03 changed by Fredrik Lundh <fredrik@pythonware.com>

  • stage changed from Accepted to Ready for checkin.

09/15/07 06:41:14 changed by mtredinnick

So, for reference, there were a few problems with this patch.

  • The first version didn't check the precision.
  • The second version didn't work on input like " 3.14".
  • The test in the second version showed that IntegerField was cleaning 3.14 (the number) to 3, rather than raising an error.

I'm committing something that fixes all of these.

09/15/07 06:45:19 changed by mtredinnick

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [6282]) Fixed #5355 -- Fixed data cleaning for DecimalField?.

In passing, fixed a problem with cleaning in IntegerField?.

Includes tests from PhiR.

09/15/07 06:47:00 changed by Fredrik Lundh <fredrik@pythonware.com>

There were an extensive discussion last night (european time); I simply assumed that tests in the final patch matched what was agreed upon.


Add/Change #5355 (DecimalField may "clean" itself and then raise an Exception)




Change Properties
Action