用法related_name查询集与Django CMS插件

时间:2016-04-04 21:55:38

标签: python django django-cms

Python 3

Django 1.9

Django-CMS 3.2.2

我有这样的事情:

models.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')

cms_plugins.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

我添加几个内联然后保存。在编辑中,所有内联都显示正常,但如果我发布页面,则只返回一个空列表。我理解它的出版系统故障,但我怎样才能使它发挥作用?

2 个答案:

答案 0 :(得分:1)

这样的事情应该有效。 @ atterratio的答案仅适用于多对多关系:

import elementtree.ElementTree as ET
with open('my.xml', 'rU') as myxml:
    ET.parse(myxml)

答案 1 :(得分:0)

需要添加

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

到我的PluginModel。 http://docs.django-cms.org/en/3.2.2/how_to/custom_plugins.html#handling-relations