trouble with forms/CreateView

4 views
Skip to first unread message

clavie...@gmail.com

unread,
Jul 16, 2018, 9:54:42 AM7/16/18
to Django users
I seem to be having trouble understanding some of how form validation works, resulting in two problems. 

I am trying to write a CreateView in which a user can type in an item number, and the program will use that instead of the item's primary key to perform the lookup. 

The docs appear to recommend overriding form_valid, but these are the issues I've been running into: 
  1. I would need duplicate code in both form_valid and form_invalid because the item number entered by the user may or may not also happen to be a primary key for a different item. As far as I've seen, Django assumes that the input is a primary key, and will call either method as appropriate based on whether or not it could find an item with that primary key.
  2. Overriding form_invalid doesn't seem to be working anyway:
    • Django appears to clean data before validating it, and I believe cleaned data is immutable. 
    • CreateView has no clean() method to override that I can find (either here or the source code itself on github)
    • I made a ModelForm to use with CreateView expressly for the clean() methods it offers, but neither clean() nor clean_<field> are being called (my breakpoints are being skipped). 
Does anybody know a way to accomplish this? How does CreateView clean its data? Any suggestions would be helpful.

Thank you!
Heather
Reply all
Reply to author
Forward
0 new messages