Hi Knut,

Actually, what would help you here is custom model field + form field
+ widget.

For example, the following combination based on Babel (http://
babel.edgewall.org/) could be used:

class DecimalWidget(forms.TextInput):
    def render(self, name, value, attrs=None):
        locale=get_current_language()
        if value is None: value = ""
        if value and isinstance(value, basestring):
            try:
                value = parse_decimal(value, locale=locale)
            except NumberFormatError:
                pass
        if value is not "" and not isinstance(value, basestring):
            value = format_decimal(value, self.format, locale=locale)
        return super(type(self), self).render(name, value, attrs)

class FloatField(forms.FloatField):
    widget = DecimalWidget
    def __init__(self, format=None, *args, **kwargs):
        super(type(self), self).__init__(*args, **kwargs)
        self.format = self.widget.format = format
    def clean(self, value):
        locale=get_current_language()
        if value != "":
            try:
                value = force_unicode(parse_decimal(value,
locale=locale))
            except NumberFormatError:
                raise forms.ValidationError(self.error_messages
['invalid'])
        return super(type(self), self).clean(value)

class LocalizedFloatField(models.FloatField):
    def formfield(self, **kwargs):
        defaults = {'form_class': FloatField}
        defaults.update(kwargs)
        return super(FloatField, self).formfield(**defaults)

Regards,
Aidas Bendoraitis

On Apr 17, 9:04 am, Knut Nesheim <knut.nesh...@mtab.se> wrote:
> Hi all,
>
> I have a model with several FloatFields. Based on this, ModelForm  
> creates some form.FloatFields.These fields require the user to type  
> '3.2' instead of '3,2', which is the custom here(Sweden). We need to  
> allow the comma somehow, for obvious usability reasons.
>
> My first thought was to write a custom clean method on the form, which  
> would do a string replace, but this isn't working. The FloatFields  
> clean method is run before any custom method. If there's a comma in  
> the field, that method will raise an exception and my custom method  
> never gets called.
>
> Any ideas for a solution would be welcome.
>
> Thanks,
> Knut
> --
> Knut Nesheim  | MTAB Transport & Spedition AB
> Box 20164 | SE-161 02 Bromma | Visiting address: Ranhammarsvägen 26
> Phone: +46 8 54 600 153
> E-mail: knut.nesh...@mtab.se  | Web:www.mtab.se
>
> Vi utför alla uppdrag på grundval av NSAB 2000.
> All transactions are carried out according to our standard conditions  
> covered by NSAB2000
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to