views:

161

answers:

1

Quite often I need to filter some form data before using it (saving to database etc.) Let's say I want to strip whitespaces and replace repeating whitespaces with a single one in most of the text fields, in many forms. It's not difficult to do this using clean_<fieldname> methods:

# Simplified model with two text fields
class MyModel(models.Model):
    title = models.CharField()
    description = models.CharField()

# Model-based form
class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def clean_title(self):
        title = self.cleaned_data['title']
        return re.sub(r'\s{2,}', ' ', title.strip())

    def clean_description(self):
        description = self.cleaned_data['description']
        return re.sub(r'\s{2,}', ' ', description.strip())

It does exactly what I need, and has a nice side effect which I like: if user enters only whitespaces, the field will be considered empty and therefore invalid (if it is required) and I don't even have to throw a ValidationError.

The obvious problem here is code duplication. Even if I'll create some function for that, say my_text_filter, I'll have to call it for every text field in all my forms:

from myproject.filters import my_text_filter

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def clean_title(self):
        return my_text_filter(self.cleaned_data['title'])

    def clean_description(self):
        return my_text_filter(self.cleaned_data['description'])

The question: is there any standard and simple way in Django (I use version 1.2 if that matters) to do this (like, for example, by adding property validators = {'title': my_text_filter, 'description': my_text_filter} to MyModel), or at least some more or less standard workaround? I've read about form validation and validators in the documentation, but couldn't find what I need there.

A: 

Hi, as i know Django don't have any native solutions to resolve you question. I can show only how i resolve this problem for myself. I advice you to use decorator for you ModelForm. Here the working code(also i use logging module):

#DECORATOR
def applyValidators(model_form):
    def apply(*args,**kwargs):
        try:
            if hasattr(model_form.Meta.model,"validators"):
                for field_name,fnc in model_form.Meta.model.validators.items():
                    setattr(model_form,"clean_%s" % field_name,fnc)
        except Exception,err:
            logging.error(str(err))
        return model_form(*args,**kwargs)
    return apply

#VALIDATORS
def validator(*args,**kwargs):
    return "SOMEVALUE"

#MODEL
class MyModel(models.Model):
    #Your fields
    .......
    .......
    .......

    #VALIDATE DICT
    validators = {"username":validator,"email":validator}    

#MODEL FORM
@applyValidators
class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
    .......
    .......
    .......
Saff