Code

Opened 6 years ago

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

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.

Attachments (0)

Change History (1)

comment:1 Changed 6 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

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.

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.