在cms之外的django-cms占位符 - 如何使用连接的一个模型中的占位符获取相关模型?

时间:2014-02-24 23:10:33

标签: python django django-cms django-queryset django-mptt

我写了以下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,如何获得所有已连接新闻条目的列表? (对于项目详细信息页面上的反向链接)

1 个答案:

答案 0 :(得分:1)

这应该有效:

placeholders = []
for plugin in project.projectplugin_set.all():
    placeholders.append(plugin.placeholder_id)
NewsEntry.objects.filter(content__in=placeholders).distinct()