Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django中为ModelMultipleChiceField创建自定义树小部件?_Python_Django_Python 3.x_Django Widget_Django 1.10 - Fatal编程技术网

Python 如何在django中为ModelMultipleChiceField创建自定义树小部件?

Python 如何在django中为ModelMultipleChiceField创建自定义树小部件?,python,django,python-3.x,django-widget,django-1.10,Python,Django,Python 3.x,Django Widget,Django 1.10,在我的表单中,我有一个函数字段,它是modelmultipechoicefield,我想在其中添加由用户函数选择的内容。作为queryset函数字段,获取组的所有对象。有人能帮我为该字段创建如下图片所示的自定义小部件吗?在下面,您还可以看到我的小部件的html(tree\u widget.html) tree\u widget.html: {% for group in groups %} <p>{{ group }}</p> {% for task i

在我的表单中,我有一个
函数
字段,它是
modelmultipechoicefield
,我想在其中添加由用户函数选择的内容。作为
queryset
函数字段,获取
组的所有对象。有人能帮我为该字段创建如下图片所示的自定义小部件吗?在下面,您还可以看到我的小部件的html(
tree\u widget.html

tree\u 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 %}
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
class CustomTreeWidget(CheckboxSelectMultiple):
    template_name = 'tree_widget.html'
    ???
widgets.py:

{% 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 %}
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
class CustomTreeWidget(CheckboxSelectMultiple):
    template_name = 'tree_widget.html'
    ???

可能重复@JohnMoutafis的两个不同问题。也许只有标题是一样的,但问题是不同的。如果你愿意,我可以更改标题。这不是标题的问题,而是上下文的问题。如果你遵循上面链接的帖子中给出的解决方案,你将能够为你的问题构建一个解决方案。没有人将ModelMultipleChiceField与treeview一起使用。你还有其他的想法吗,我的fried?可能是@JohnMoutafis的复制品它有两个不同的问题。也许只有标题是一样的,但问题是不同的。如果你愿意,我可以更改标题。这不是标题的问题,而是上下文的问题。如果你遵循上面链接的帖子中给出的解决方案,你将能够为你的问题构建一个解决方案。没有人将ModelMultipleChiceField与treeview一起使用。你还有别的想法吗?