1

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).

enter image description here

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'
    ???
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193
  • Possible duplicate of [Django: How to build a custom form widget?](http://stackoverflow.com/questions/4707192/django-how-to-build-a-custom-form-widget) – John Moutafis May 15 '17 at 09:58
  • @JohnMoutafis Its two different questions. Maybe only titles are the same but problems are different. If you want I can change the title. – Nurzhan Nogerbek May 15 '17 at 10:13
  • it is not a matter of titles, but of context. If you follow the solutions given in the above-linked post, you will be able to construct a solution for your problem. – John Moutafis May 15 '17 at 10:15
  • No one of this answers didnt describes my problem. No one use ModelMultipleChoiceField with treeview. Do you have any other ideas my fried? – Nurzhan Nogerbek May 15 '17 at 10:24

0 Answers0