views:

2360

answers:

3

When I have a valid Django form, I can access the data with form.cleaned_data. But how do I get at the data a user entered when the form is not valid i.e., form.is_valid is false.

I'm trying to access forms within a form set, so form.data seems to just give me a mess.

+10  A: 

See http://docs.djangoproject.com/en/dev/ref/forms/validation/#ref-forms-validation

Secondly, once we have decided that the combined data in the two fields we are considering aren't valid, we must remember to remove them from the cleaned_data.

In fact, Django will currently completely wipe out the cleaned_data dictionary if there are any errors in the form. However, this behaviour may change in the future, so it's not a bad idea to clean up after yourself in the first place.

The original data is always available in request.POST.


A Comment suggests that the point is to do something that sounds like more sophisticated field-level validation.

Each field is given the unvalidated data, and either returns the valid data or raises an exception.

In each field, any kind of validation can be done on the original contents.

S.Lott
I was hoping there was a better way since that has the raw, mangled input names that the formset made up, e.g., form-1-my_input. So I'd have to do some string contortions to get at the data for the right form.
Greg
The data isn't valid. What does "right form" mean?
S.Lott
I think OP means that it would be nice to manipulate what the form parsed, even if it didn't validate: form.phone_number would return whatever value the field `phone_number` got, regardless of whether it validated correctly.
DGGenuine
Invalid data? I don't get it. Either the data's valid and can be processed or not valid. If you want to relax the rules to allow more (and perhaps do other cleanup) that's what field-level validation is for. Once the field-level validation is done, it's either valid or the whole form is useless.
S.Lott
+1  A: 

You access the data from either the field's clean() method, or from the form's clean() method. clean() is the function that determines whether the form is valid or not. It's called when is_valid() is called. In form's clean() you have the cleaned_data list when you can run through custom code to make sure it's all checked out. In the widget, you have a clean() also, but it uses a single passed variable. In order to access the field's clean() method, you'll have to subclass it. e.g.:

class BlankIntField(forms.IntegerField):
    def clean(self, value):
        if not value:
            value = 0
        return int(value)

If you want an IntField that doesn't choke on an empty value, for instance, you'd use the above.

clean() on a form kind of works like this:

def clean(self):
    if self.cleaned_data.get('total',-1) <= 0.0:
        raise forms.ValidationError("'Total must be positive")
    return self.cleaned_data

Also you can have a clean_FIELD() function for each field so you can validate each field individually (after the field's clean() is called)

nbv4
+2  A: 

You can use

<form>.['field_name'].data

This way you get the raw value assigned to the field.

Dmitry Risenberg