使用Django CMS插件的与使用相关的名称查询集

2024-05-28 23:45:39 发布

您现在位置:Python中文网/ 问答频道 /正文

Python3

德扬戈1.9

Django CMS 3.2.2版

我有这样的想法:

你知道吗型号.py你知道吗

class PluginModel(CMSPlugin):
    title = models.CharField(max_length=60)

class InlineModel(models.Model):
    title = models.CharField(max_length=60)
    plugin_key = models.ForeignKey('PluginModel' related_name='breakpoints')

厘米_插件.py你知道吗

class InlineModelInline(admin.StackedInline):
    model = InlineModel

class PluginModelPlugin(CMSPluginBase):
    model = PluginModel
    inlines = [InlineModelInline,]

    def render(self, context, instance, placeholder):
        context = super(CarouselPlugin, self).render(context, instance, placeholder)
        print(instance.breakpoints.all()) #for simple debug

我添加一些内联线然后保存。在edit中,所有的内联线看起来都是正常的,但是如果我发布页面,只会返回一个空列表。我知道这是什么出版系统故障,但我如何才能使它工作?你知道吗


Tags: instancepymodeltitlemodelscontextrenderlength
2条回答

需要添加

def copy_relations(self, oldinstance):
    self.breakpoints = oldinstance.breakpoints.all()

我的插件模型。http://docs.django-cms.org/en/3.2.2/how_to/custom_plugins.html#handling-relations

这样的办法应该行得通。@atterratio的答案只适用于多对多关系:

class PluginModel(CMSPlugin):
    title = models.CharField(max_length=60)

    def copy_relations(self, oldinstance):
        for breakpoint in oldinstance.breakpoints.all():
            breakpoint.pk = None
            breakpoint.plugin_key = self
            breakpoint.save()

相关问题 更多 >

    热门问题