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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:29:26+00:00 2026-05-21T00:29:26+00:00

So the title is a bit obtuse, I know, but I couldn’t think of

  • 0

So the title is a bit obtuse, I know, but I couldn’t think of a more succinct way to state it. Here’s the issue:

I’ve created two proxy models for “user types”, both inheriting from django.contrib.auth.User. Each has a custom manager limiting the queryset to items belonging to a particular Group. Specifically, there’s a PressUser which is any user belonging to the “Press” group and StaffUser which is any user in any other group than “Press”.

The issue is that when I add ‘groups’ to list_filters on my StaffUsers modeladmin, the resulting filter options are every group available, including “Press”, and not just groups available to StaffUsers.

I’ve research a bit online and came up with a custom filterspec that should produce the behavior I want, but the problem is that the User model’s ‘groups’ attribute is actually a related_name applied from the Group model. As a result, I can’t attach my filterspec to ‘groups’ in my proxy model.

Is there any other way to apply the filterspec? Alternatively, is there a better approach to filtering the items returned by the default filterspec?

  • 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-21T00:29:27+00:00Added an answer on May 21, 2026 at 12:29 am

    So, I was able to solve my own problem. For those that might run into a similar situation, here are the steps:

    The approach I took is to modify the change_list.html template and manually filter out the items I didn’t want to be included. There’s quite a number of changes to make, though.

    First, add a changelist_view method to your ModelAdmin:

    # myproject/account/admin.py
    
    class StaffUserAdmin(models.ModelAdmin):
        ...
        def changelist_view(self, request, extra_context=None):
            groups = Group.objects.exclude(name__in=['Press',]).values_list('name')
            extra_context = {
                'groups': [x[0] for x in groups],
            }
            return super(StaffUserAdmin, self).changelist_view(request,
                extra_context=extra_context)
    

    Basically, all we’re doing here is passing in the filtered list of Groups we want to use into the context for the template.

    Second, create a change_list.html template for your app.

    # myproject/templates/admin/auth/staffuser/change_list.html
    
    {% extends "admin/change_list.html" %}
    
    {% load admin_list %}
    {% load i18n %}
    {% load account_admin %}
    
    {% block filters %}
    
        {% if cl.has_filters %}
        <div id="changelist-filter">
            <h2>{% trans 'Filter' %}</h2>
            {% for spec in cl.filter_specs %}
                {% ifequal spec.title 'group' %}
                    {% admin_list_group_filter cl spec groups %}
                {% else %}
                    {% admin_list_filter cl spec %}
                {% endifequal %}
            {% endfor %}
        </div>
        {% endif %}
    
    {% endblock filters %}
    

    This one deserves a little explanation. First, the template tag loads: admin_list is used for the default Django template tag responsible for rendering the filters, admin_list_filter, i18n is used for trans, and account_admin is for my custom template tag (discussed in a sec), admin_list_group_filter.

    The variable spec.title holds the title of the field that’s being filtered on. Since I’m trying to alter how the Groups filter is displayed, I’m checking if it equals ‘groups’. If it does, then I use my custom template tag, otherwise, it falls back to the default Django template tag.

    Third, we create the template tag. I basically just copied the default Django template tag and made the necessary modifications.

    # myproject/account/templatetags/account_admin.py
    
    from django.template import Library
    
    register = Library()
    
    def admin_list_group_filter(cl, spec, groups):
        return {'title': spec.title, 'choices' : list(spec.choices(cl)), 'groups': groups }
    admin_list_group_filter = register.inclusion_tag('admin/auth/group_filter.html')(admin_list_group_filter)
    

    The only things that I’ve changed here are adding a new argument to the method called ‘groups’ so I can pass in my filtered list of groups from before, as well as adding a new key to the dictionary to pass that list into the context for the template tag. I’ve also changed the template the tag uses to a new one that we’re about to create now.

    Fourth, create the template for the template tag.

    # myproject/templates/admin/auth/group_filter.html
    
    {% load i18n %}
    <h3>{% blocktrans with title as filter_title %} By {{ filter_title }} {% endblocktrans %}</h3>
    <ul>
    {% for choice in choices %}
        {% if choice.display in groups %}
        <li{% if choice.selected %} class="selected"{% endif %}>
            <a href="{{ choice.query_string|iriencode }}">{{ choice.display }}</a></li>
        {% endif %}
    {% endfor %}
    </ul>
    

    No big surprises here. All we’re doing is putting all the pieces together. Each choice is a dictionary with all the values needed to construct the filter link. Specifically, choice.display holds the actual name of the instance that will be filtered by. Obviously enough, I’ve set up a check to see if this value is in my filtered list of groups I want to show, and only render the link if it is.

    So, it’s a bit involved but works remarkably well. Just like that, you have a list of filters that is exactly what you want instead of the default ones generated by Django.

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

Sidebar

Related Questions

I know the title is a bit weird, but I was unable to think
So I know I butchered the title a bit but if someone looks here
Ok I know the title is a bit confusing but here is my problem:
The title may be bit confusing but here is what I am facing I
Ok, the title is a bit confusing, but here's my predicament. I've made a
The title is a bit awkward but I couldn't found a better one. My
The title is a bit confusing but here's what I'm after: I have a
I know the title is a bit vague. But what I'm trying to achieve
I know the title's a bit wordy, but I don't know how else to
(Title sounds a bit too fancy but I couldn't find a match so we'll

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.