This is a two-part question
Problem description
I have a form with a field that uses ModelSelect2TagWidget (that accepts free-text as an input). when the user enters a number as free-text, it is converted to the object whose id is the number entered as a free-text. For example, if the user enters "11" as free-text
and the form is submitted, the entry is automatically converted to the value whose id is 11 in the database
On the other hand, if the user chooses one of the available choices (not a free-text), the id of the selected choice in the select2 field is sent in the request as in the screenshot.
The problem is when the form is submitted, I have no way to distinguish whether the submitted value is an id or a free-text number entered by the user.
To solve this problem, I am thinking about sending the text of the input field instead of the id as follows
- How can I send the text of the selected choice instead of the id in the request?
- As this is a ModelSelect2TagWidget field, it accepts free-text. Is there a better way to distinguish between the id of a selected choice or if the user enters a number as a free-text?
Thank you for your help!
I used the following solution. Further links AutoResponseView
Subclassed
AutoResponseView
from django-select2. Inresponse_obj
set the "id
" value to the field of the model you want to use. In my case wasobj.name
(instead ofobj.pk
in AutoResponseView itself)in a new file
select2_classes.py
inside my appIn urls.py inside app folder
In forms.py, added a widget to the corresponding field