Opened 14 years ago

Closed 14 years ago

Last modified 13 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: 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)

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

Download all attachments as: .zip

Change History (4)

by Bernd Schlapsi <brot@…>, 14 years ago

Attachment: patch_and_test.diff added

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

comment:1 by Jannis Leidel, 14 years ago

Triage Stage: UnreviewedAccepted

comment:2 by jkocherhans, 14 years ago

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 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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