Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#12539 closed (worksforme)

Error while validating non required IntegerField with localflavor form field

Reported by: Bernd Schlapsi <brot@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
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


There is a validation error when validating a non required IntegerField and using a localflavor Field for the ModelForm

This is my example

from django import forms
from 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':''})

In [3]: f.full_clean()

In [4]: f.errors
Out[4]: {'zip': [u'This value must be a float.']}

Attachments (1)

patch_and_test.diff (3.0 KB) - added by Bernd Schlapsi <brot@…> 9 years ago.
Patch for this issue and a testcase wich fails without the patch

Download all attachments as: .zip

Change History (4)

Changed 9 years ago by Bernd Schlapsi <brot@…>

Attachment: patch_and_test.diff added

Patch for this issue and a testcase wich fails without the patch

comment:1 Changed 9 years ago by Jannis Leidel

Triage Stage: UnreviewedAccepted

comment:2 Changed 9 years ago by jkocherhans

Resolution: worksforme
Status: newclosed

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.

comment:3 Changed 7 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

Note: See TracTickets for help on using tickets.
Back to Top