Opened 7 years ago

Closed 7 years ago

#7939 closed (duplicate)

ModelChoiceField gets an error when you pass an String instead of an integer

Reported by: Oriol Martí Owned by: nobody
Component: Forms Version: 0.96
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


When you have a ModelChoiceField field and you pass an String instead of an Integer you have an error.
If a user sends a string (GET or POST) to the page an error occurs. Here is an example of this error:

from django import newforms as forms
from django.utils.translation import ugettext as _
from satchmo.l10n.models import Country

class AdvSearch (forms.Form):

... country = forms.ModelChoiceField(Country.objects.all(), required=False, label = _("Country"))

f = AdvSearch({"country":"gg"})
if f.is_valid():

... print "VALID"
... else:
... print "NO VALID"
Traceback (most recent call last):

File "<stdin>", line 1, in <module>
File "/home/sinuhe/lib/python2.5/django/newforms/", line 95, in is_valid

This error can be corrected if you check the parameters that the user is sending, but I think that the best behavior when this happens is the same if you send an Integer that not exists in the Query.

Change History (1)

comment:1 Changed 7 years ago by ElliottM

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.
Back to Top