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.
<th><label for="id_underwriters">Underwriter(s):</label></th>
<td>
**<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?
So you can see the value= clauses in the individual options - those are what should be submitted when the form is posted.
What does the view look like for this?
Also, the network tab of the developer tools will show what’s being submitted by the form. It’s worth looking at that to verify what data is being submitted.
In short, something, somewhere along the line is trying to manipulate these selections by the display value and not the pk.
Do you have any JavaScript involved with this widget?
Looking at what you’ve posted more closely, it looks like you may have Select2 involved here? (Am I reading that correctly?)
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.
Validates that every id in the given list of values exists in the queryset.
None that I know of, and hopefully none at all. (We consider that type of structure to be an anti-pattern.)
Notice how the Django Admin puts a “+” sign with related fields to allow for the entry of additional data. You can’t just add data within the widget itself.