Opened 10 years ago

Closed 10 years ago

#3238 closed enhancement (duplicate)

Add FloatField in newforms

Reported by: jay.baird@… Owned by: Adrian Holovaty
Component: Forms Version: master
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: UI/UX:

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 Changed 10 years ago by jay.baird@…

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 Changed 10 years ago by James Bennett

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 Changed 10 years ago by jay.baird

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 Changed 10 years ago by Adrian Holovaty

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

comment:5 Changed 10 years ago by Adrian Holovaty

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

comment:6 Changed 10 years ago by Waylan Limberg <waylan@…>

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 Changed 10 years ago by adurdin@…

Cc: adurdin@… added

comment:8 Changed 10 years ago by adurdin@…

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

comment:9 Changed 10 years ago by adurdin@…

Should this be closed in favour of #2365?

comment:10 Changed 10 years ago by Jay Baird

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 Changed 10 years ago by Malcolm Tredinnick

See #4004 for an alternative patch.

comment:12 Changed 10 years ago by Malcolm Tredinnick

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