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

The Archive Base Latest Questions

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

i have a model : class Foo(models.Model): attribute = models.IntegerField() user = models.ForeignKey(user) and

  • 0

i have a model :

class Foo(models.Model):
   attribute = models.IntegerField()
   user = models.ForeignKey(user)

and a model admin :

class FooAdmin(admin.ModelAdmin):
   list_filter = ('attribute',)

In the admin, i’m only displaying Foo objects of the active user but the attribute filter give me all possible attributes even the ones which don’t return any objects for that user. At the end, i have a lot of different filter values, and most of them are useless for the user. How can i limit those filter values to the ones which match the user.

hope it’s understandable

Cheers

Solution :
In the admin.py of your app put :

from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec
from django.contrib.auth.models import User
from sets import Set

class CustomChoiceFilterSpec(ChoicesFilterSpec):

    def __init__(self, f, request, params, model, model_admin):
        super(CustomChoiceFilterSpec, self).__init__(f, request, params, model,
                                                   model_admin)
    self.lookup_kwarg = '%s__attribute__exact' % f.name
    self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    self.objects = list(Set([i.attribute for i in model.objects.filter(foo__user = request.user)])) # This is the magic line :) !

    def choices(self, cl):
        yield {'selected': self.lookup_val is None,
                'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
                'display': ('All')}
        for val in self.objects:
            yield {'selected': smart_unicode(val) == self.lookup_val,
                    'query_string': cl.get_query_string({self.lookup_kwarg: val.attribute}),
                    'display': val.attribute}

    def title(self):
        return "Attribute"
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'compact_filter', False), CustomChoiceFilterSpec)) 

the last line says that this type of filter will apply only to objects which have ‘compact_filter’ as an attribute.

So we put in our model Foo (models.py) :

attribute.compact_filter = True
  • 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-20T16:21:30+00:00Added an answer on May 20, 2026 at 4:21 pm

    Unfortunately, overriding those list_filters is not easy.

    There’s massive progress on writing our own list filters as you can see on this ticket: http://code.djangoproject.com/ticket/5833 but won’t make it for 1.3.

    All I can suggest is to start using this new vocabulary: “FilterSpec” to find code snippets from various people who have been able to get their custom FilterSpecs working on SO and on the googles.

    Custom Filter in Django Admin on Django 1.3 or below
    http://djangosnippets.org/snippets/1051/

    I will take this as an opportunity to mess around with FilterSpecs myself!

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

Sidebar

Related Questions

No related questions found

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.