Django

Code

Ticket #3238 (closed: duplicate)

Opened 1 year ago

Last modified 1 year ago

Add FloatField in newforms

Reported by: jay.baird@gmail.com Assigned to: adrian
Milestone: Component: django.newforms
Version: SVN Keywords: newforms fields floatfield
Cc: jay.baird@gmail.com, waylan@gmail.com, adurdin@gmail.com Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

Attachments

Change History

01/05/07 20:53:42 changed by jay.baird@gmail.com

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?

01/05/07 22:55:54 changed by ubernostrum

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.

01/06/07 00:43:59 changed 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.

01/10/07 17:37:42 changed by adrian

  • summary changed from A FloatField for those who need one in newforms to [patch] Add FloatField in newforms.

01/17/07 16:49:37 changed by adrian

  • summary changed from [patch] Add FloatField in newforms to Add FloatField in newforms.
  • stage changed from Unreviewed to Design decision needed.

01/24/07 14:23:00 changed by Waylan Limberg <waylan@gmail.com>

  • cc changed from jay.baird@gmail.com to jay.baird@gmail.com, waylan@gmail.com.

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?

01/26/07 09:04:15 changed by adurdin@gmail.com

  • cc changed from jay.baird@gmail.com, waylan@gmail.com to jay.baird@gmail.com, waylan@gmail.com, adurdin@gmail.com.

01/28/07 03:59:30 changed by adurdin@gmail.com

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

02/22/07 16:32:11 changed by adurdin@gmail.com

Should this be closed in favour of #2365?

02/22/07 16:34:59 changed 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?

05/14/07 03:15:36 changed by mtredinnick

See #4004 for an alternative patch.

05/20/07 10:44:13 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to duplicate.

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


Add/Change #3238 (Add FloatField in newforms)




Change Properties
Action