Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 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: UI/UX:

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@…> 4 years ago.
Patch for this issue and a testcase wich fails without the patch

Download all attachments as: .zip

Change History (4)

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

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

comment:1 Changed 4 years ago by jezdez

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 4 years ago by jkocherhans

  • Resolution set to worksforme
  • Status changed from new to 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.

comment:3 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.