Opened 17 years ago

Closed 17 years ago

#3238 closed enhancement (duplicate)

Add FloatField in newforms

Reported by: jay.baird@… 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 jay.baird@…, 17 years ago

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?

comment:2 by James Bennett, 17 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 jay.baird, 17 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 Adrian Holovaty, 17 years ago

Summary: A FloatField for those who need one in newforms[patch] Add FloatField in newforms

comment:5 by Adrian Holovaty, 17 years ago

Summary: [patch] Add FloatField in newformsAdd FloatField in newforms
Triage Stage: UnreviewedDesign decision needed

comment:6 by Waylan Limberg <waylan@…>, 17 years ago

Cc: waylan@… 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 adurdin@…, 17 years ago

Cc: adurdin@… added

comment:8 by adurdin@…, 17 years ago

Yes, I'll incorporate this with the new patch for #2365 as the newforms FloatField.

comment:9 by adurdin@…, 17 years ago

Should this be closed in favour of #2365?

comment:10 by Jay Baird, 17 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:11 by Malcolm Tredinnick, 17 years ago

See #4004 for an alternative patch.

comment:12 by Malcolm Tredinnick, 17 years ago

Resolution: duplicate
Status: newclosed

Closing as a dupe of #2365, since this patch has been incorporated there and looks reasonable.

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