Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 318835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:36:23+00:00 2026-05-12T08:36:23+00:00

How can I add a custom filter to django admin (the filters that appear

  • 0

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??
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:36:23+00:00Added an answer on May 12, 2026 at 8:36 am

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

    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))
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I add custom action in generator.yml that point to an action in
how can i add a custom view to the right click menu of every
How can I add a custom filter or create a custom search field with
I am trying to add an additional custom field to a django model. I
I am struggeling with Magento custom options. In Magento you can add custom options
Can someone explain how can I add custom font, css classes styles to Contribute?
Arrays have a "length" property by default. Can I add custom properties to them?
Users on my site can add nodes of a custom type (let's call it
is it possible to add custom data to the results so it can be
I would like to add custom attributes to a MySQL table which I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.