I have an existing crispy form. I want to use django-select2 to replace an existing multi-select with a Select2 field with a maximum of two selections. I know from this post that I need to include 'data-maximum-selection-length' as a widget attribute.
Here is the field code I am adding:
forms.ModelMultipleChoiceField(
required=True,
queryset=MyModel.objects.all(),
widget=s2forms.Select2MultipleWidget(attrs={'data-maximum-selection-length': 2}))
I have included {{ form.media.css }} in the head of my template and {{ form.media.js }} before the tag. Edit: I have path('select2/', include('django_select2.urls')),
included on my urls.py.
The field is not formatted to appear as a select2 dropdown, it looks like a standard multiselect field.
This is what I'm hoping to obtain: ... this is how it looks:
I'd be appreciative of any ideas!
References:
- django-select2 docs
- django ModelMultipleChoiceField docs
- django crispy forms docs
This is not an answer to the above question, but I am including this as the alternative solution to the original issue I implemented.
Essentially I sidestepped the crispy forms issue by injecting the Select2 field I needed in via HTML as a fieldset.
I am not going to accept this answer in case someone has an actual solution that still uses the Select2 object as intended.