如何访问 Django-CMS 插件的父模型

How to access the parent model of a Django-CMS plugin

我创建了 2 个 django-cms 插件,一个父 "Container" 可以包含多个子 "Content" 插件。

当我保存子插件时,我想访问父插件的模型。

from cms.plugin_pool import plugin_pool
from cms.plugin_base import CMSPluginBase

from .models import Container, Content

class ContainerPlugin(CMSPluginBase):
    model = Container
    name = "Foo Container"
    render_template = "my_package/container.html"
    allow_children = True
    child_classes = ["ContentPlugin"]

class ContentPlugin(CMSPluginBase):
    model = content
    name = "Bar Content"
    render_template = "my_package/content.html"
    require_parent = True
    parent_classes = ["ContainerPlugin"]
    allow_children = True

    def save_model(self, request, obj, form, change):
        response = super(ContentPlugin, self).save_model(
            request, obj, form, change
        )

        # here I want to access the parent's (container) model, but how?

        return response

plugin_pool.register_plugin(ContainerPlugin)
plugin_pool.register_plugin(ContentPlugin)

obj 是当前的插件实例,所以我可以得到这个模型的所有属性,但我不知道如何访问父插件模型。有 obj.parent,但据我所知,它不是插件实例。还尝试使用 self.cms_plugin_instanceobj.parent.get_plugin_instance() 但没有成功。

有什么建议吗?

给定一个插件实例,instance.get_plugin_instance()方法returns一个元组包含:

  1. instance - 插件实例
  2. plugin - 关联的插件 class 实例 get_plugin_instance

所以像这样获取父对象:

instance, plugin_class = object.parent.get_plugin_instance()

选项 1

查看 CMSPluginBase 的源代码,您也许可以使用 get_child_classes 的实现。不幸的是,该方法实际上只有 returns class 个名称,因此您不能直接使用它。但我认为它实际上确实迭代了子实例以获取 class 名称:

def get_child_classes(self, slot, page):
    from cms.utils.placeholder import get_placeholder_conf

    template = page and page.get_template() or None

    # config overrides..
    ph_conf = get_placeholder_conf('child_classes', slot, template, default={})
    child_classes = ph_conf.get(self.__class__.__name__, self.child_classes)
    if child_classes:
        return child_classes
    from cms.plugin_pool import plugin_pool
    installed_plugins = plugin_pool.get_all_plugins(slot, page)
    return [cls.__name__ for cls in installed_plugins]

您感兴趣的是这两行:

    from cms.plugin_pool import plugin_pool
    installed_plugins = plugin_pool.get_all_plugins(slot, page)

选项 2

另一种方法(我在我的代码中使用的方法)是使用信号,尽管这也需要找到正确的对象。恕我直言,代码的可读性不是很好(请参阅我挥之不去的内联评论),但它确实有效。它是前一段时间写的,但我仍在使用它与 django-cms 3.2.3.

占位符名称实际上是您为占位符配置的名称。将其移至设置或某处当然更可取。不过,我不确定为什么我没有这样做。

我会对您的解决方案感兴趣!

# signals.py
import itertools
import logging

from cms.models import CMSPlugin
from cms.plugin_pool import plugin_pool
from django.db import ProgrammingError
from django.db.models.signals import post_save

logger = logging.getLogger(__name__)
_registered_plugins = [CMSPlugin.__name__]


def on_placeholder_saved(sender, instance, created, raw, using, update_fields, **kwargs):
    """
    :param sender: Placeholder
    :param instance: instance of Placeholder
    """
    logger.debug("Placeholder SAVED: %s by sender %s", instance, sender)
    # TODO this is totally ugly - is there no generic way to find out the related names?
    placeholder_names = [
        'topicintro_abstracts',
        'topicintro_contents',
        'topicintro_links',
        'glossaryentry_explanations',
    ]
    fetch_phs = lambda ph_name: _fetch_qs_as_list(instance, ph_name)
    container = list(itertools.chain.from_iterable(map(fetch_phs, placeholder_names)))
    logger.debug("Modified Placeholder Containers %s (%s)", container, placeholder_names)
    if container:
        if len(container) > 1:
            raise ProgrammingError("Several Containers use the same placeholder.")
        else:
            # TODO change modified_by (if possible?)
            container[0].save()

def _fetch_qs_as_list(instance, field):
    """
    :param instance: a model
    :param field: optional field (might not exist on model)
    :return: the field values as list (not as RelatedManager)
    """
    qs = getattr(instance, field)
    fields = qs.all() if qs else []
    return fields

def on_cmsplugin_saved(sender, instance, created, raw, using, update_fields, **kwargs):
    """
    :param sender: CMSPlugin or subclass
    :param instance: instance of CMSPlugin
    """
    plugin_class = instance.get_plugin_class()
    logger.debug("CMSPlugin SAVED: %s; plugin class: %s", instance, plugin_class)
    if not plugin_class.name in _registered_plugins:
        post_save.connect(on_cmsplugin_saved, sender=plugin_class)
        _registered_plugins.append(plugin_class.name)
        logger.info("Registered post_save listener with %s", plugin_class.name)
    on_placeholder_saved(sender, instance.placeholder, created, raw, using, update_fields)

def connect_existing_plugins():
    plugin_types = CMSPlugin.objects.order_by('plugin_type').values_list('plugin_type').distinct()
    for plugin_type in plugin_types:
        plugin_type = plugin_type[0]
        if not plugin_type in _registered_plugins:
            plugin_class = plugin_pool.get_plugin(plugin_type)
            post_save.connect(on_cmsplugin_saved, sender=plugin_class)
            post_save.connect(on_cmsplugin_saved, sender=plugin_class.model)
            _registered_plugins.append(plugin_type)
            _registered_plugins.append(plugin_class.model.__name__)
    logger.debug("INIT registered plugins: %s", _registered_plugins)

post_save.connect(on_cmsplugin_saved, sender=CMSPlugin)

你必须在某处设置这些信号。我在我的 urls.py 中执行此操作,但应用程序配置可能是适合它的位置? (我试图避免应用程序配置。)

# This code has to run at server startup (and not during migrate if avoidable)
try:
    signals.connect_existing_plugins()
except db.utils.ProgrammingError:
    logger.warn('Failed to setup signals (if your DB is not setup (not tables), you can savely ignore this error.')

由于子插件总是继承 上下文。在父模板中,您可以执行以下操作:

{% with something=instance.some_parent_field %}
  {% for plugin in instance.child_plugin_instances %}
          {% render_plugin plugin %}
  {% endfor %}
{% endwith %}

并在您的子模板中使用东西