In my form I have function
field which is ModelMultipleChoiceField
where I want to add selected by user functions. As queryset
function field take all objects of Group
. Can someone help me to create custom widget as the picture below for that field? Also here below you can see html of my widget (tree_widget.html
).
tree_widget.html:
{% for group in groups %}
<p>{{ group }}</p>
{% for task in group.task_set.all %}
<p>{{ task }}</p>
{% for function in task.function_set.all %}
<p>
<input type="checkbox" name="option" value="{{ function }}">{{ function }}
</p>
{% endfor %}
{% endfor %}
{% endfor %}
forms.py:
class RequirementForm(forms.ModelForm):
function = forms.ModelMultipleChoiceField(required=True, widget=CustomTreeWidget, queryset=Group.objects.none())
class Meta:
model = Requirement
fields = ('function',)
def __init__(self, all_groups, all_user_characteristics, *args, **kwargs):
super(RequirementForm, self).__init__(*args, **kwargs)
self.fields['function'].queryset = all_groups # I take all Group objects from view
widgets.py:
class CustomTreeWidget(CheckboxSelectMultiple):
template_name = 'tree_widget.html'
???