Following the code in this post, I was able to group the options in a ModelMultipleChoiceField based on another field. As described in this post, I used a custom widget (literally the one that is in the post):
Grouping CheckboxSelectMultiple Options in Django
so now the form is rendering the way I'd like it to, but I was hoping to improve on the formatting by using crispy forms. however, when I added {% load crispy_forms_tags %} and {{form|crispy}} it rendered the form without the grouping that I worked so hard to achieve...
does anyone know how if its possible to apply crispy forms to a form with a custom widget and preserve what the widget does?
thanks!
Django-crispy-forms is an application that helps to manage Django forms. It allows adjusting forms' properties (such as method, send button or CSS classes) on the backend without having to re-write them in the template.
django-crispy-forms provides you with a |crispy filter and {% crispy %} tag that will let you control the rendering behavior of your Django forms in a very elegant and DRY way. Have full control without writing custom form templates.
No, this won't be possible because crispy forms never calls the render
method of the widget.
An alternative is to override the template for the widget, in this case bootstrap/layout/checkboxselectmultiple.html
, to use the grouping as required.
https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templates/bootstrap/layout/checkboxselectmultiple.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With