You can see above that the underwriters':['1','2','test4'] are the options and the 1, 2 are the id’s of existing underwriter objects while the ‘test4’ is a new item coming from the form. I am saving this test4 object to the db before the form gets checked right after the POST request comes in.
This form validation that fails is happening before the code explicitly calls is_valid() on the form.
**<ul class="errorlist"><li>“test4” is not a valid value.</li></ul>**
Check the html being rendered for these widgets. The value clause should be the PK of that model while the display value should be the output of the __str__ method.
What is being rendered for these entries?
Are you doing anything with a custom default manager or using a non-integer PK in the Underwriters model?
Ok, that’s the problem. You’re not going to be able to do that with this widget. The value must be the primary key of the entry. You’d need to submit the desired new entry as an AJAX call, re-render that widget with the new set of entries, and replace the existing widget with the new one returned by the server.
(You’d also have the option of creating a custom widget that would process the values as supplied in a different manner than how the standard widget works.)
Interesting ok just so I understand you are saying that the form.SelectMultiple widget on the form field will not allow for the submission/validation of new values that are not already in the db model?
Not quite correct, it’s the form.ModelMultipleChoiceField that won’t allow it. (Yes, I was wrong by referencing it as the widget being the issue instead of it being the form field). That field validates the entries based on the value attribute being valid entries of the queryset being used for the widget.