Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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
Django models Django cms插件mptt根据配置按节点过滤以显示新子树_Django Models_Django Cms_Django Filter - Fatal编程技术网

Django models Django cms插件mptt根据配置按节点过滤以显示新子树

Django models Django cms插件mptt根据配置按节点过滤以显示新子树,django-models,django-cms,django-filter,Django Models,Django Cms,Django Filter,我正在尝试为django cms实现一个插件,它显示一个链接树。我想做的是根据用户在我的CMS中选择的配置过滤这棵树。因此,基于他在配置中选择的节点,我希望能够仅显示该子树 这是我的模特 from django.db import models from mptt.models import MPTTModel, TreeForeignKey from cms.models.pluginmodel import CMSPlugin class Section(MPTTModel): n

我正在尝试为django cms实现一个插件,它显示一个链接树。我想做的是根据用户在我的CMS中选择的配置过滤这棵树。因此,基于他在配置中选择的节点,我希望能够仅显示该子树

这是我的模特

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
from cms.models.pluginmodel import CMSPlugin

class Section(MPTTModel):
    name = models.CharField(max_length=25, unique=True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)

    class MPTTMeta:
        order_insertion_by = ['name']

    def __str__(self):
        return self.name


class SectionConfig(CMSPlugin):
    root_shown = models.ForeignKey("Section")
    title = models.CharField(default="Usefull Links", max_length=25)
这是我的cms_plugins.py:

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from cms.models.pluginmodel import CMSPlugin
from django.utils.translation import ugettext_lazy as _
from links_plugin.models import Section, SectionConfig

class LinksPlugin(CMSPluginBase):
    name = _("Links Tree Plugin")
    model = SectionConfig
    render_template = "links.html"
    cache = False

    def render(self, context, instance, placeholder):
        context['instance'] = instance
        context['nodes'] = Section.objects.all()
        return context

plugin_pool.register_plugin(LinksPlugin)
这是我的模板/links.html

<div class='container-fluid'>
    <h1>Liens Utiles</h1>
    {% load mptt_tags %}
    <ul class="root">
        {% recursetree nodes %}
            <li>
                {{ node.name }}
                {% if not node.is_leaf_node %}
                    <ul class="children">
                        {{ children }}
                    </ul>
                {% endif %}
            </li>
        {% endrecursetree %}
    </ul>
</div>
到一个过滤器,该过滤器将基于我的

root_shown = models.ForeignKey("Section")
问题是我不知道如何使用FK引用我的节对象并找到我的新根节。从那时起,我想我可以使用get_genderantsinclude_self=True来重建这个新的子树并显示节点列表。我错了吗?如何引用所需的节点


如果可能的话,ELI5

您会想要这样的东西:

contect['nodes'] = instance.root_shown.get_descendants(include_self=True)
contect['nodes'] = instance.root_shown.get_descendants(include_self=True)