Closed ozgur-as closed 1 year ago
Example 17.2.2 works. Submitted data is send to the server. So, if you believe that's a bug, please create a reproducible setup.
okay, I've reproduced the same error with the state demo. I've completed all the steps from 22 and edited state.py modelchoicefields to following to preselect items:
state = models.ModelChoiceField(
label="State",
queryset=State.objects.all(),
widget=Selectize(
search_lookup='name__icontains',
),
initial=2
)
county = models.ModelChoiceField(
label="County",
queryset=County.objects.all(),
widget=Selectize(
search_lookup=['name__icontains'],
filter_by={'state': 'state__id'},
),
initial=3
)
the selectbox of county on the browser gets unselected. but like i said on my previous post, the html selected tags are transferred correctly:
<option value="2" selected>Alaska</option>
<option value="3" selected>Barbour (AL)</option>
if I do a manual setValue call on the tomselect object of the element in chrome console, i can make it select the option, that's my current workaround.
i am thinking the reason for the bug is that the second field's options array is not populated when it reads the selected html tag. because the second field is dependent on the first one, so the array is filtered with an ajax call when the page loads and it cannot find the value it needs to select it.
you never pointed me on the forked branch of your repository, therefore I was not able to reproduce this bug.
https://github.com/ozgur-as/django-formset/tree/selectize-bug
you can reproduce the bug with this branch on testapp/default/state example.
Thanks for reporting and apologies for the long delay fixing it.
The current fix in main branch should fix this. Please recheck. I then will release it as version 1.1.1
Btw.: I used id=70
for "Anchorage (AK)" instead of id=3
as in your example because the latter is "Barbour" which is located in Alabama and not Alaska. Using state=2
and county=3
also works, although it's an invalid choice. This then however is enforced by the server and hence in its responsibility.
Follow up: Version 1.1.1 has been released. This should fix the problem you described.
it's still not fixed. the part where you used selectizemultiple renders preselected on the browser while the county option is still rendering not preselected. tried it with chrome, edge, forefox and webkit to see if it's a problem at the browser side, all rendered the same.
you're right, there is a typo.
In testapp/forms/state.py
please change line 41
widget=SelectizeMultiple(
search_lookup=['name__icontains'],
- filter_by={'states': 'state__id'},
+ filter_by={'state': 'state__id'},
),
initial=[3, 70, 2940],
)
yup, you're right about the typo, but the typo is in line 27. changing state to states in line 27 fixed it.
i'm using 2 modelchoicefields in my modelform definition, the second one is filtered by the first one's id like it's given in the example(17.2.2. Filtering Select Options).
when looking at an editview for the said model, the second field loses its preselected choice. the form is prefilled properly when i remove the filter_by definition.
i am thinking it's a js problem since when i look at the source of the page, the proper option's selected tag is rendered in html.
thanks in advance!