<tfoot id='ApOwX'></tfoot>
    <legend id='ApOwX'><style id='ApOwX'><dir id='ApOwX'><q id='ApOwX'></q></dir></style></legend>

      <bdo id='ApOwX'></bdo><ul id='ApOwX'></ul>
  1. <small id='ApOwX'></small><noframes id='ApOwX'>

    1. <i id='ApOwX'><tr id='ApOwX'><dt id='ApOwX'><q id='ApOwX'><span id='ApOwX'><b id='ApOwX'><form id='ApOwX'><ins id='ApOwX'></ins><ul id='ApOwX'></ul><sub id='ApOwX'></sub></form><legend id='ApOwX'></legend><bdo id='ApOwX'><pre id='ApOwX'><center id='ApOwX'></center></pre></bdo></b><th id='ApOwX'></th></span></q></dt></tr></i><div id='ApOwX'><tfoot id='ApOwX'></tfoot><dl id='ApOwX'><fieldset id='ApOwX'></fieldset></dl></div>

      Django 1.3 或更低版本的 Django Admin 中的自定义过滤器

      Custom Filter in Django Admin on Django 1.3 or below(Django 1.3 或更低版本的 Django Admin 中的自定义过滤器)
    2. <small id='AASzw'></small><noframes id='AASzw'>

        <tbody id='AASzw'></tbody>
    3. <i id='AASzw'><tr id='AASzw'><dt id='AASzw'><q id='AASzw'><span id='AASzw'><b id='AASzw'><form id='AASzw'><ins id='AASzw'></ins><ul id='AASzw'></ul><sub id='AASzw'></sub></form><legend id='AASzw'></legend><bdo id='AASzw'><pre id='AASzw'><center id='AASzw'></center></pre></bdo></b><th id='AASzw'></th></span></q></dt></tr></i><div id='AASzw'><tfoot id='AASzw'></tfoot><dl id='AASzw'><fieldset id='AASzw'></fieldset></dl></div>
      <tfoot id='AASzw'></tfoot>
            • <bdo id='AASzw'></bdo><ul id='AASzw'></ul>

              • <legend id='AASzw'><style id='AASzw'><dir id='AASzw'><q id='AASzw'></q></dir></style></legend>
                本文介绍了Django 1.3 或更低版本的 Django Admin 中的自定义过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如何将自定义过滤器添加到 django admin(显示在模型仪表板右侧的过滤器)?我知道包含基于该模型的字段的过滤器很容易,但是像这样的计算"字段呢:

                How can I add a custom filter to django admin (the filters that appear on the right side of a model dashboard)? I know its easy to include a filter based on a field of that model, but what about a "calculated" field like this:

                class NewsItem(models.Model):
                    headline = models.CharField(max_length=4096, blank=False)
                    byline_1 = models.CharField(max_length=4096, blank=True)
                    dateline = models.DateTimeField(help_text=_("date/time that appears on article"))
                    body_copy = models.TextField(blank=False)
                
                    when_to_publish = models.DateTimeField(verbose_name="When to publish",  blank=True, null=True)
                
                    # HOW CAN I HAVE "is_live" as part of the admin filter?  It's a calculated state!!
                    def is_live(self):
                        if self.when_to_publish is not None:
                            if ( self.when_to_publish < datetime.now() ):
                                return """ <img alt="True" src="/media/img/admin/icon-yes.gif"/> """
                        else:
                            return """ <img alt="False" src="/media/img/admin/icon-no.gif"/> """      
                
                    is_live.allow_tags = True
                

                <小时>

                class NewsItemAdmin(admin.ModelAdmin):
                    form = NewsItemAdminForm
                    list_display = ('headline', 'id', 'is_live')
                    list_filter = ('is_live')  #  how can i make this work??
                

                推荐答案

                感谢 gpilotino 让我朝着正确的方向推进.

                Thanks to gpilotino for giving me the push into the right direction for implementing this.

                我注意到问题的代码使用日期时间来确定它的活动时间.所以我使用了 DateFieldFilterSpec 并对其进行了子类化.

                I noticed the question's code is using a datetime to figure out when its live . So I used the DateFieldFilterSpec and subclassed it.

                from django.db import models
                from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec,DateFieldFilterSpec
                from django.utils.encoding import smart_unicode
                from django.utils.translation import ugettext as _
                from datetime import datetime
                
                class IsLiveFilterSpec(DateFieldFilterSpec):
                    """
                    Adds filtering by future and previous values in the admin
                    filter sidebar. Set the is_live_filter filter in the model field attribute
                    'is_live_filter'.    my_model_field.is_live_filter = True
                    """
                
                    def __init__(self, f, request, params, model, model_admin):
                        super(IsLiveFilterSpec, self).__init__(f, request, params, model,
                                                               model_admin)
                        today = datetime.now()
                        self.links = (
                            (_('Any'), {}),
                            (_('Yes'), {'%s__lte' % self.field.name: str(today),
                                       }),
                            (_('No'), {'%s__gte' % self.field.name: str(today),
                                    }),
                
                        )
                
                
                    def title(self):
                        return "Is Live"
                
                # registering the filter
                FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'is_live_filter', False),
                                               IsLiveFilterSpec))
                

                要使用你可以把上面的代码放到一个filters.py中,然后在你想要添加过滤器的模型中导入

                To use you can put the above code into a filters.py, and import it in the model you want to add the filter to

                这篇关于Django 1.3 或更低版本的 Django Admin 中的自定义过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                • <tfoot id='4XuaR'></tfoot>
                  <i id='4XuaR'><tr id='4XuaR'><dt id='4XuaR'><q id='4XuaR'><span id='4XuaR'><b id='4XuaR'><form id='4XuaR'><ins id='4XuaR'></ins><ul id='4XuaR'></ul><sub id='4XuaR'></sub></form><legend id='4XuaR'></legend><bdo id='4XuaR'><pre id='4XuaR'><center id='4XuaR'></center></pre></bdo></b><th id='4XuaR'></th></span></q></dt></tr></i><div id='4XuaR'><tfoot id='4XuaR'></tfoot><dl id='4XuaR'><fieldset id='4XuaR'></fieldset></dl></div>

                        <tbody id='4XuaR'></tbody>
                        <legend id='4XuaR'><style id='4XuaR'><dir id='4XuaR'><q id='4XuaR'></q></dir></style></legend>

                      • <small id='4XuaR'></small><noframes id='4XuaR'>

                          <bdo id='4XuaR'></bdo><ul id='4XuaR'></ul>