Django 嵌套内联

Django nested inlines

我正在开发自定义 Django CMS 插件,遇到需要嵌套内联的情况。下面是我的模型结构。

class Link(NavLink):
    card = models.ForeignKey('CardPanel', related_name='card_links')

class CardPanel(models.Model):
    title = models.CharField(max_length=50)
    image = FilerImageField(null=True, blank=True, related_name="navigation_vertical_link_image")
    link_description = HTMLField(blank=True, null=True, max_length=150)
    button_link_internal = PageField(blank=True, null=True)
    button_link_external = models.URLField(blank=True, null=True)
    plugin = models.ForeignKey('Panel')

class Panel(CMSPlugin):
    pass

我理想中需要的是嵌套内联。因此,由于 Link 模型与 CardPanel 有 m:1 关系,而 CardPanel 与 Panel 模型有 m:1 关系,我希望能够添加包含多个 Link 模型的多个 CardPanel。通过 Django 中的 ModelAdmin 实现此目的的最佳方法是什么?

如果它是您在这里创建的插件,那么从 3.0 开始,这些只是 managed by the frontend:

In the new system, Placeholders and their plugins are no longer managed in the admin site, but only from the frontend.

因此,CMSPlugins 有各种属性,我认为您会发现它们对此很有用,包括 CMS 附带的一些标准插件。如果它用于插件,您也不需要在模型上指定 plugin 属性。

我会调整你的插件 class & 相应的模型更像;

# models.py
from cms.models.fields import PlaceholderField

class CardPanel(CMSPlugin):
    title = models.CharField(max_length=50)
    image = FilerImageField(
        null=True,
        blank=True,
        related_name="navigation_vertical_link_image"
    )
    content = PlaceholderField('card_panel_content')

# cms_plugins.py

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

from .models import CardPanel


@plugin_pool.register_plugin
class CardPanel(CMSPluginBase):
    """ Plugin to contain card panels """
    model = CardPanel
    parent_classes = ['Panel']  # Include this if a card panel only exists in a panel

@plugin_pool.register_plugin
class Panel(CMSPluginBase):
    """ Plugin to contain card panels """
    model = CMSPlugin
    allow_children = True  # Allow the Panel to include other plugins
    child_classes = ['CardPanel']

通过在您的 CardPanel 上包含一个 PlaceholderField,您可以为模型实例呈现一个占位符并将 CMS 插件添加到该实例,就像将它们添加到页面中一样。这样,您可以根据需要添加尽可能多的 link 插件和 that plugin,如果您不使用它,则允许页面 links 或外部 links .

模板中的占位符字段是这样呈现的;

{% load cms_tags %}

{% render_placeholder card_panel_instance.content %}

PlaceholderField也可以用admin注册; http://docs.django-cms.org/en/latest/how_to/placeholders.html#admin-integration