'Django removes my custom html attributes from option tag
I am trying to add custom data-* attributes to the option tag in a select element. I have my custom template (widget) which is used by Django, but it seems like Django removes my custom attributes. somewhere in further steps
My custom option template:
widgets/tree_option_template.html
<option value="{{ widget.attrs.lft|stringformat:'s' }}"
data-test="test"
>{{ widget.label }} - {{ widget.attrs.rght|stringformat:'s' }}</option>
Custom widget:
class MultiChoiceFilterWidget(forms.SelectMultiple):
"""
TODO.
Based on FilteredSelectMultiple
"""
option_inherits_attrs = True
option_template_name = "widgets/tree_option_template.html"
...
Usage in admin.py:
class UserAdminForm(forms.ModelForm):
class Meta:
model = User
fields = "__all__"
read_projects = CustomTreeNodeMultipleChoiceField(
queryset=Project.objects.filter(disabled=False),
required=False,
widget=MultiChoiceFilterWidget(verbose_name="Projects", is_stacked=False),
)
When I am changing e.g. value attribute then it changes in DOM as well but my custom attributes are not available in HTML:

As we can see there is no data-test attribute...
Any idea why my custom tags are not visible in HTML?
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
