#12539 closed (worksforme)
Error while validating non required IntegerField with localflavor form field
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev | 
| Severity: | Keywords: | ||
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
There is a validation error when validating a non required IntegerField and using a localflavor Field for the ModelForm
This is my example models.py
from django import forms
from django.contrib.localflavor.at.forms import ATZipCodeField
from django.db import models
   
class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()
    zip = models.PositiveSmallIntegerField(blank=True, null=True)
class PersonForm(forms.ModelForm):
    zip = ATZipCodeField(required=False)
    class Meta:
        model = Person
This is how you can reproduce the error
In [1]: from app.models import PersonForm
In [2]: f = PersonForm({'first_name':'Max', 'last_name':'Mustermann', 'email':'a.b@test.com'})
In [3]: f.full_clean()
In [4]: f.errors
Out[4]: {'zip': [u'This value must be a float.']}
      Attachments (1)
Change History (4)
by , 16 years ago
| Attachment: | patch_and_test.diff added | 
|---|
comment:1 by , 16 years ago
| Triage Stage: | Unreviewed → Accepted | 
|---|
comment:2 by , 16 years ago
| Resolution: | → worksforme | 
|---|---|
| Status: | new → closed | 
I don't think auto-converting empty strings and unicode objects to None in the model fields (like your patch does) is the right approach. It seems like using a CharField as your model field, or making ATZipCodeField perform the conversion to an int via a to_python method would be a better idea.
Also, I added the test from your patch to trunk r12271 without the fixes, and it passed. I this issue may have been fixed by [12206]. Please reopen if you have a test that fails on or after r12271.
Patch for this issue and a testcase wich fails without the patch