views:

67

answers:

1

This is about the Format Localization feature that was implemented in Django 1.2.

In order to use this feature, you must add a localize=True parameter to all your form fields. I am trying to implement this localization in my app but the problem is that I am creating my forms dynamically by using the inlineformset_factory method that Django provides, so I cannot simply add a new parameter to the form field.

So I tried to enable this feature by default in all models, without needing to add a new parameter for all fields. I created a BaseInlineFormSet subclass and hard-coded the parameter in it.

class MyBaseInlineFormSet(BaseInlineFormSet):
    def __init__(self, *args, **kwargs):
        super(MyBaseInlineFormSet, self).__init__(*args, **kwargs)
        for form in self.forms:
            for key, field in form.fields.iteritems():
                if field.__class__ == forms.DecimalField:
                    form.fields[key].localize = True

That worked only 50%. When submitted, the forms are being validated correctly by Django now (it's accepting commas instead of only dot) but the fields are still being displayed incorrectly.

I guess I could javascript my way out of this problem, but I prefer to avoid doing that.

Any ideas on how to solve this?

Thanks,

Cesar Canassa

A: 

Hi -

I've have not used it - (still to picka project to develop in Django) - but it seens to be the case of subclassing -

Instead of having your fields inheriting from forms.DecimalField, make them be a:

class LocalizedDecimalField(forms.DecimalField):
    localize = True
jsbueno