django cms插件,显示选中特定值的模型
我制作了一个显示文章的模型,当你创建一篇文章时,你可以选择这篇文章是否是特色文章 这就是我在文章模型中的基本内容:django cms插件,显示选中特定值的模型,django,django-cms,Django,Django Cms,我制作了一个显示文章的模型,当你创建一篇文章时,你可以选择这篇文章是否是特色文章 这就是我在文章模型中的基本内容: class Article(ModelMeta, TranslatableModel): taints_cache = True """ Press article element, """ date_created = models.DateTimeField(auto_now_add=True) date_modified =
class Article(ModelMeta, TranslatableModel):
taints_cache = True
"""
Press article element,
"""
date_created = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
date_realization = models.DateField(_('Realised in'),
default=timezone.now)
image = FilerImageField(verbose_name=_('Featured image'), blank=True,
null=True,
on_delete=models.SET_NULL,
related_name='image_press_article',
help_text=_('Set if the article will be featured'))
sources = models.ManyToManyField(ArticleSource, verbose_name=_('Source'),
blank=False, null=True, related_name='sources_press_article')
regions = models.ManyToManyField(Country, verbose_name=_('Country of the article'),
blank=True, null=True,
related_name='regions_press_article')
global_regions = models.BooleanField('Global', default=True)
featureArticle = models.BooleanField(_('Feature'), help_text=_('Feature this article'), default=False)
然后,我创建了一个显示特色文章的插件。
但问题是,在django插件管理中,我允许用户选择要显示的文章(最多3篇)。
但是在这个选择列表中,我所有的文章都列出了
我想的是只列出在我的插件管理中被选中为“特色”的文章。而不是所有的文章
以下是我的cms_插件的模型:
class FeaturedArticlePlugin(CMSPlugin):
selected_article = SortedManyToManyField(Article, blank=True, verbose_name=_('Selected articles'),
help_text=_('Select the featured articles to display'))
def __str__(self):
return u'%s Selected articles' % self.selected_article.all()
def copy_relations(self, oldinstance):
self.selected_article = oldinstance.selected_article.all()
在我的cms_plugins.py中:
class PressPlugin(CMSPluginBase):
module = 'Press'
class PressFeaturedArticlePlugin(PressPlugin):
module = _('Press')
name = _('Press feature')
model = FeaturedArticlePlugin
render_template = 'djangocms_press/plugins/feature_article.html'
number_article = 3
def render(self, context, instance, placeholder):
"""
Get a list of selected_articles
"""
selected_article = instance.selected_article.all()
number_selected_article = selected_article.count()
feature_article_list = list(selected_article[:self.number_article])
context['instance'] = instance
context['feature_article_list'] = feature_article_list
return context
plugin_pool.register_plugin(PressFeaturedArticlePlugin)
所以,我相信这并不复杂,但我不能指出这一点
有人有线索吗
编辑
据我所知,唯一与所有文章的展示有关的是这一行:
selected_article = SortedManyToManyField(Article, blank=True, verbose_name=_('Selected articles'),
help_text=_('Select the featured articles to display'))
因此,我想做的是用featureArticle=True筛选这篇选中的文章但是怎么做呢?我不太确定我是否遗漏了什么,但是,你不能在这里应用一个过滤器吗
selected_article = instance.selected_article.all().filter(featureArticle=true)
number_selected_article = selected_article.count()
还是后面的线路有问题
feature_article_list = list(selected_article[:self.number_article])
如果你的问题是选择额外的物品,也许你需要按日期订购,只选择必要的
feature_article_list = list(Articles.all().order_by('-created')[:self.number_article - number_selected_article]
哪个只会选择额外的必需品
编辑:你的处境让我想起了我曾经遇到的一个问题。所以我会让你参考过去帮助过我的那一页,以防万一你能找到答案
编辑2:“我创建了一个显示特色文章的插件。但问题是,在django插件管理中,我允许用户选择他想要显示的文章(最多3篇)。但是在这个选择列表中,我的所有文章都列出了。”
如果所有的文章都展示在那里,不是很好吗?如果没有全部显示,您如何从中进行选择?不清楚您的问题是什么?我更新了以使其更清楚。它现在做什么?密码会被破解吗?或者它会显示所有的文章,而不仅仅是那些特色。它会显示所有的文章,而不仅仅是那些特色!嗯,我仍然有所有的文章显示。我试过:
instance.selected\u article.all().filter(featureArticle=True)
和instance.selected\u article.filter(featureArticle=True)
你能验证只有特色文章在selected\u articles字段中吗?嗯,feature\u article\u列表是模板使用的上下文,我不确定它会影响管理员!如果我错了,请纠正我。是的,特色产品在列表中等一下,您的问题是FeaturedArticlePlugin还是PressFeaturedArticlePlugin?