Django-cms plugin mptt filtering by node to display new subtree, based on config -
i trying implement plugin django-cms shows tree of of links. want filter tree based on config user chooses in cms. based on node chooses in config want able display sub-tree.
here models.py
from django.db import models mptt.models import mpttmodel, treeforeignkey 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)
here cms_plugins.py:
from cms.plugin_base import cmspluginbase cms.plugin_pool import plugin_pool cms.models.pluginmodel import cmsplugin django.utils.translation import ugettext_lazy _ 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)
and here templates/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>
so issue giving right set of nodes context. in cms_plugins.py id change
context['nodes'] = section.objects.all()
to filter build subtree based on
root_shown = models.foreignkey("section")
the problem don't know how use fk reference section object , find new root section. thought use get_descendants(include_self=true) rebuild new sub-tree , display list of nodes. wrong? how reference desired node?
if possible eli5
you'll want this:
contect['nodes'] = instance.root_shown.get_descendants(include_self=true)
Comments
Post a Comment