Opened 16 years ago

Closed 16 years ago

#7941 closed (worksforme)

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: no UI/UX: no

Description

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/forms.py", 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 by Jacob, 16 years ago

Resolution: worksforme
Status: newclosed

Looking at the code for ModelChoiceField, it doesn't care which type you use; it just tries to look up an object with the value as pk. If you can post more information -- a complete traceback would help a lot -- maybe we can track this down, but right now it looks like it's probably a bug in your code.

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