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 8440999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:27:03+00:00 2026-06-10T08:27:03+00:00

I would like to do something if one modeladmin is passed and do another

  • 0

I would like to do something if one modeladmin is passed and do another thing if another modeladmin is passed. But it seems that the modeladmin doesn’t get passed as a parameter in list_filter while it does get passed in actions in django admin. why is this so?

from datetime import date

from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import SimpleListFilter

class DecadeBornListFilter(SimpleListFilter):
    # Human-readable title which will be displayed in the
    # right admin sidebar just above the filter options.
    title = _('decade born')

    # Parameter for the filter that will be used in the URL query.
    parameter_name = 'decade'

    def lookups(self, request, model_admin):
        """
        Returns a list of tuples. The first element in each
        tuple is the coded value for the option that will
        appear in the URL query. The second element is the
        human-readable name for the option that will appear
        in the right sidebar.
        """
        return (
            ('80s', _('in the eighties')),
            ('90s', _('in the nineties')),
        )

    def queryset(self, request, queryset):
        """
        Returns the filtered queryset based on the value
        provided in the query string and retrievable via
        `self.value()`.
        """
        # Compare the requested value (either '80s' or '90s')
        # to decide how to filter the queryset.
        if self.value() == '80s':
            return queryset.filter(birthday__gte=date(1980, 1, 1),
                                    birthday__lte=date(1989, 12, 31))
        if self.value() == '90s':
            return queryset.filter(birthday__gte=date(1990, 1, 1),
                                    birthday__lte=date(1999, 12, 31))

For example, in the example above, i would like to do something different if it is a student which is to check the birthday is between 90’s or 2000’s. But it is the parent, i would like to check if the birthday is between 70’s or 80’s? Just assume that different modeladmin will be passed. How do i include the modeladmin as parameter to do these changes? Need some guidance on this…

  • 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-06-10T08:27:05+00:00Added an answer on June 10, 2026 at 8:27 am

    You could set self.model_admin:

    class DecadeBornListFilter(SimpleListFilter):
        #[...]
    
        def lookups(self, request, model_admin):
            self.model_admin = model_admin
            # ...
    
        def queryset(self, request, queryset):
            # here you can use self.model_admin
    

    Or, use inheritance:

    class BaseDecadeBornListFilter(SimpleListFilter):
        # [...]
    
    
    class DecadeBornListFilter1(BaseDecadeBornListFilter):
        # [...]
    
    
    class DecadeBornListFilter2(BaseDecadeBornListFilter):
        # [...]
    
    
    class StudentModelAdmin1(admin.ModelAdmin):
        list_filter = (DecadeBornListFilter1,)
    
    
    class StudentModelAdmin2(admin.ModelAdmin):
        list_filter = (DecadeBornListFilter2,)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In jquery that is. I would like something that works as the success-pararameter, but
I would like to combine two __m128 values to one __m256 . Something like
I'm looking to compare two varchars in SQL, one would be something like Cafe
How would one go about defining something like this in an xsd? <start> <request
I would like do something like that. list_of_urls = ['http://www.google.fr/', 'http://www.google.fr/', 'http://www.google.cn/', 'http://www.google.com/', 'http://www.google.fr/',
I would like to do something like this: abstract class Foo { public function
I would like to do something like this in HTML5 where I have something
I would like to recreate something like this if ( arg1 || arg2 ||
I would like to run something like: select * from table where field in
I would like to do something like the following with spark. <viewdata model=IList[[string]] />

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.