Opened 8 years ago

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


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 sciyoshi 8 years ago.
Simple fix for catching ValueError

Download all attachments as: .zip

Change History (5)

comment:1 Changed 8 years ago by anonymous

  • Cc medoslav@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 8 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 8 years ago by guettli

The dpaste snippet has expired.

To burp_: Please make the code available again.

Changed 8 years ago by sciyoshi

Simple fix for catching ValueError

comment:4 Changed 8 years ago by kmtracey

  • Resolution set to duplicate
  • Status changed from new to closed

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