Opened 18 years ago
Closed 18 years ago
#3238 closed enhancement (duplicate)
Add FloatField in newforms
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | normal | Keywords: | newforms fields floatfield |
Cc: | jay.baird@…, waylan@…, adurdin@… | Triage Stage: | Design decision needed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Not sure if this should be wrapped up in a patch, or what, but I just thought I'd put this out there in case anyone wanted to use it.
import re import decimal from django.newforms import * float_regex = re.compile( r'\d+.\d+' ) class FloatField(RegexField): def __init__(self, max_digits=None, decimal_places=None,max_value=None, min_value=None, required=True, widget=None, label=None, initial=None): self.max_value, self.min_value = decimal.Decimal(max_value or 'inf'), decimal.Decimal(min_value or '-inf') self.max_digits, self.decimal_places = max_digits, decimal_places RegexField.__init__(self, regex=float_regex, error_message="Please enter a valid decimal number", required=required, widget=widget, label=label, initial=initial) def clean(self, value): super(FloatField, self).clean(value) digits,decimals = value.split(".") if not self.required and value in EMPTY_VALUES: return u'' try: value = decimal.Decimal(value) except: raise ValidationError("Enter a decimal number") if self.max_value is not None and value > self.max_value: raise ValidationError("Ensure that value is less than or equal to %s" % self.max_value) if self.min_value is not None and value < self.min_value: raise ValidationError("Ensure that value is greater than or equal to %s" % self.min_value) if self.max_digits is not None and len(digits) > self.max_digits: raise ValidationError("Ensure that the number of digits is less than or equal to %s" % self.max_digits) if self.decimal_places is not None and len(decimals) > self.decimal_places: raise ValidationError("Ensure that the number of decimal places is less than or equal to %s" % self.decimal_places) return value
Change History (12)
comment:1 by , 18 years ago
comment:2 by , 18 years ago
I can't speak to this suggestion one way or another, but in the past patches have been rejected which relied on the decimal
module, in order to maintain compatibility with (the still widely-deployed) Python 2.3.
comment:3 by , 18 years ago
Yeah, since after looking at the way FloatField in the model.Fields handles things I'd replace that with float() for sure now.
comment:4 by , 18 years ago
Summary: | A FloatField for those who need one in newforms → [patch] Add FloatField in newforms |
---|
comment:5 by , 18 years ago
Summary: | [patch] Add FloatField in newforms → Add FloatField in newforms |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:6 by , 18 years ago
Cc: | added |
---|
This probably needs to be considered in relation to ticket #2365 (models.FloatField should be renamed). Whatever happens there should happen here so that FloatFields can match with FloatFields and DecimalFields with DecimalFields
comment:7 by , 18 years ago
Cc: | added |
---|
comment:8 by , 18 years ago
Yes, I'll incorporate this with the new patch for #2365 as the newforms FloatField.
comment:10 by , 18 years ago
Not sure. It's there and ready to go, but it still hasn't been looked at by Adrian who's the principle on newforms. Anyone at PyCon want to get together and talk about it?
comment:12 by , 18 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Closing as a dupe of #2365, since this patch has been incorporated there and looks reasonable.
I'm not sure which is preferred in Django itself, Decimal or float(). If you're trying to keep backwards compatibility with 2.3 then this version is out, but with float() you get the dreaded 3.4 being expressed as 3.99999999998.
Any thoughts?