我写了以下cms插件。有了这个,用户可以添加从一个新闻条目到项目的连接。
cms_plugins.py:
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
class CMSProjectPlugin(CMSPluginBase):
model = ProjectPlugin
name = _("Project")
render_template = "CMSProjectPlugin.html"
def render(self, context, instance, placeholder):
context.update({
'project':instance.project,
'object':instance,
'placeholder':placeholder
})
return context
plugin_pool.register_plugin(CMSProjectPlugin)
型号:
from django.db import models
from cms.models.fields import PlaceholderField
class Project(models.Model):
title=models.CharField(_('Title'),max_length=250)
slug = AutoSlugField(populate_from='title')
class ProjectPlugin(CMSPlugin):
project = models.ForeignKey(Project)
class NewsEntry(models.Model):
title=models.CharField(_('Title'),max_length=250)
content = PlaceholderField('news_content')
如果我有项目ID,如何获得所有已连接新闻条目的列表? (对于项目详细信息页面上的反向链接)
答案 0 :(得分:1)
这应该有效:
placeholders = []
for plugin in project.projectplugin_set.all():
placeholders.append(plugin.placeholder_id)
NewsEntry.objects.filter(content__in=placeholders).distinct()