views:

59

answers:

1

Prior to today, I've been using Django 1.1. To ensure I'm keeping up with the times, I decided to update my Django environment to use Django 1.2.3. Unfortunately, I've encountered an issue.

The following code did not raise a ValueError in 1.1:

        instance = FormClass(
            request.POST,
            instance=existing_instance
        ).save(commit=False)

However, now that I've upgraded, it raises a ValueError each time. I have a SSN field that I'm submitting as part of my Form and I strip out the dashes prior to doing an instance.save() call. Unfortunately, the ValueError occurs because Django thinks my SSN value is too long (it's expecting 9 characters and it's receiving 11 -- 123-45-6789).

I've looked through the Django docs and I couldn't find anything relating to this change. Any idea what's going on? I've always thought the purpose of the "commit=False" parameter was to allow pre-processing of data before saving the information.

Am I missing something?

+3  A: 

According to the 1.2 docs on the save() method, "If you call save() with commit=False, then it will return an object that hasn't yet been saved to the database." So I'm not sure why there would have been a change in functionality, but it's possible that in 1.1 validation/check code ran only when an object was saved to the database, and in 1.2 it happens earlier (before the object is saved).

In any case, if you're cleaning data that is being entered in a form, you should probably be cleaning it in the FormClass:

def clean_ssn(self):
    data = re.sub(r'[^0-9]','',self.cleaned_data['ssn'])
    #validation code here
    return data

That should at least fix the problem...

Jordan Reiter
def clean_ssn(self): ...Custom cleaners are of the form clean_{{attribute}}.
Chris C
Right, right, I knew that. A simple error that I'll fix shortly.
Jordan Reiter