Django嵌套内联

问题描述 投票:0回答:1

我正在开发一个自定义的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实现这一目标的最佳方法是什么?

django django-admin django-cms
1个回答
1
投票

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

在新系统中,Placeholders及其插件不再在管理站点中管理,而是仅从前端管理。

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

我会调整你的插件类和相应的模型有点像;

# 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']

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

占位符字段在模板中呈现,如下所示;

{% load cms_tags %}

{% render_placeholder card_panel_instance.content %}

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

© www.soinside.com 2019 - 2024. All rights reserved.