使用 related_name 查询集与 Django CMS 插件
Usage related_name queryset with Django CMS plugin
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
我添加了一些内联然后保存。在编辑中,所有内联显示正常,但如果我发布页面,则只返回一个空列表。我知道这是什么发布系统故障,但我怎样才能让它工作?
需要添加
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
像这样的东西应该有用。 @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()
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
我添加了一些内联然后保存。在编辑中,所有内联显示正常,但如果我发布页面,则只返回一个空列表。我知道这是什么发布系统故障,但我怎样才能让它工作?
需要添加
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
像这样的东西应该有用。 @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()