Opened 12 years ago

Closed 11 years ago

#8974 closed (duplicate)

ModelChoiceField raises "ValueError: invalid literal for int() with base 10: 'abc'" when non-numerical value is supplied

Reported by: burp_ Owned by: nobody
Component: Uncategorized Version: 1.0
Severity: Keywords:
Cc: medoslav@…, bronger@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When I have a Form with a ModelChoiceField, and supply this field with a non-numerical value the specified error occurs on form.is_valid() as it tries to convert 'abc' with int(). The whole thing is reproducible with this code:

Attachments (1)

8974.patch (912 bytes) - added by Samuel Cormier-Iijima 12 years ago.
Simple fix for catching ValueError

Download all attachments as: .zip

Change History (5)

comment:1 Changed 12 years ago by anonymous

Cc: medoslav@… added

comment:2 Changed 12 years ago by Torsten Bronger <bronger@…>

Cc: bronger@… added

I can confirm this. Instead of the ValueError, a ValidationError should be raised. In django/forms/, the try blocks in the clean methods of ModelChoiceField and ModelMultipleChoiceField should also catch ValueError exceptions and turn them into ValidationErrors.

comment:3 Changed 12 years ago by Thomas Güttler

The dpaste snippet has expired.

To burp_: Please make the code available again.

Changed 12 years ago by Samuel Cormier-Iijima

Attachment: 8974.patch added

Simple fix for catching ValueError

comment:4 Changed 11 years ago by Karen Tracey

Resolution: duplicate
Status: newclosed

Consolidating dupes of this problem under one ticket, #9209 chosen since that's the one I happened upon first.

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