django cms插件,用于显示具有特定值的模型 [英] django cms plugin that display model with specific value checked

查看:58
本文介绍了django cms插件,用于显示具有特定值的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个显示文章的模型,当您创建文章时,可以选择是否将其作为精选文章.

I made a model that displays articles and when you create an article you have the possibility to choose if this article will be a featured one.

所以这基本上就是我的Article模型中的内容:

So this is basically what I have in my Article model :

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个).但是在此选择列表中,列出了我所有的文章.

Then, I created a plugin that displays the featured articles. But the thing is, in the django plugin admin I let the user the possibility to choose which article he wants to display (with a maximum of 3). But in this choosing list, all my articles are listed.

我想要的是,仅列出插件管理员中的被选中为精选"的文章.而不是拥有所有文章.

What I want to, is to list only the articles that are checked as "featured", in my plugin admin. Instead of having all the articles.

这是我的cms_plugin模型所具有的:

Here what I have with my cms_plugin's model :

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中:

And in my 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)

所以,我相信这没什么复杂,但我无法指出.

So, I am sure it's nothing complicated but I can't point this out.

有人知道吗?

编辑据我了解,与所有文章的显示有关的唯一事情是此行:

EDIT From what I understand, the only thing that concern the display of all articles is this line :

selected_article = SortedManyToManyField(Article, blank=True, verbose_name=_('Selected articles'),
                                       help_text=_('Select the featured articles to display'))

所以我想做的是使用featureArticle = True过滤selected_article.但是怎么做?

So what I am suppose to do is to filter this selected_article with the featureArticle=True. But how to do it ?

推荐答案

我不太确定我是否缺少某些东西,但是,您不能只在此处应用过滤器吗?

I am not quite sure if I am missing something, but, couldn't you just apply a filter here?

selected_article = instance.selected_article.all().filter(featureArticle=true)
number_selected_article = selected_article.count()

还是后面的行有问题?

feature_article_list = list(selected_article[:self.number_article])

如果您的问题是选择多余的文章,也许您需要按日期排序并仅选择必要的文章?

If your problem is selecting the extra articles, maybe you need to order them by date and select only the necessary?

feature_article_list = list(Articles.all().order_by('-created')[:self.number_article - number_selected_article]

哪个只会选择其他必需品?

Which will only select the extra necessaries?

您的情况使我想起了我曾经遇到的一个问题.因此,请您参考过去对我有帮助的页面,以防万一您设法弄清楚.

Your situation kind of reminds me of a problem I once had. So I'll refer you to the same page that helped me in the past just in case you'd manage to figure it out.

限制django管理员更改权限

我创建了一个显示特色文章的插件.但是,在django插件管理员中,我让用户可以选择他想显示的文章(最多3个).但是在此选择列表中,列出了我的所有文章."

Edit 2 : "I created a plugin that displays the featured articles. But the thing is, in the django plugin admin I let the user the possibility to choose which article he wants to display (with a maximum of 3). But in this choosing list, all my articles are listed."

如果所有文章都显示在那里,可以吗?如果没有全部显示,如何选择它们?

Isn't it ok if all the articles are displayed there? How can you choose among them if they are not all displayed?

这篇关于django cms插件,用于显示具有特定值的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆