[英]django-cms placeholder outside cms - how to get related model using the placeholder from the one model that is connected?
我已經編寫了以下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,如何獲得所有已連接新聞條目的列表? (對於項目詳細信息頁面上的反向鏈接)
這應該工作:
placeholders = []
for plugin in project.projectplugin_set.all():
placeholders.append(plugin.placeholder_id)
NewsEntry.objects.filter(content__in=placeholders).distinct()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.