Bitten by a nasty Django trunk change

http://code.djangoproject.com/ticket/12521

I checked out the latest build of django-trunk today and ran into a nasty change regarding how forms are validated when they’re based on models (thus, ModelForms). I have a ModelForm with fields specified as excluded (a Boolean flag, and a text field that never needs editing), and when I send form data to that model form from a template using POST data, it complains about not having those excluded fields (it throws me a UnresolvableValidationError). There’s a ticket on the Django Trac about it and whether or not it should be introduced in 1.2 (as it breaks a ton of existing code… including mine :)). Essentially, it’s been introduced to fix errors when form data is validated agains a ModelForm that may not be valid against the model from the database.

This came into play for me when I had a field, department_text, that the model said is required, but the ModelForm had it excluded in the form’s Meta class. That was probably a bad design decision on my part, so I set the model field to accept blank values, and set the form where it was required (a public facing form) to be required (by specifying an overridden form field for it).

But here lies another problem: my BooleanField. It’s set as True when a confirmation email has been sent regarding a particular checkout ticket. Otherwise, it’s defaulted to False. It’s not something a user needs to edit in a form, so it’s disabled in the ModelForm. Since it’s not present in the ModelForm, it’s not present in the template as well as the POST data that comes from that template. I then get the UnresolvableValidationError stating that the BooleanField must either be True or False. Setting it to a NullBooleanField didn’t work either. Wonderful. >_<

Temporarily, I removed it from the ModelForm’s excluded list and added it into the form in its current state (a checked or unchecked checkbox, depending on the data). A less than desirable solution to be sure… but for now that’s all I could figure out. If anybody else has an idea on how to tackle it, I’m all ears.

That is, until the Django developers decide what to do about it.