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

  • Home
  • SEARCH
  • 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 6647529
In Process

The Archive Base Latest Questions

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

I have a simple Django model like: class Person(models.Model): referrer = models.ForeignKey(‘self’, null=True) …

  • 0

I have a simple Django model like:

class Person(models.Model):
    referrer = models.ForeignKey('self', null=True)
    ...

In this model’s ModelAdmin, how would I allow it to be filtered by whether or not referrer is null? By default, adding referrer to list_filter causes a dropdown to be shown that lists every person record, which may be in the hundreds of thousands, effectively preventing the page from loading. Even if it loads, I still can’t filter by the criteria I want.

i.e. How would I modify this so that the dropdown only lists “All”, “Null”, or “Not Null” choices?

I’ve seen some posts that claim to accomplish something similar using custom FilterSpec subclasses, but none of them explain how to use them. The few I’ve seen appear to apply to all fields in all models, which I wouldn’t want. Moreover, there’s zero documentation for FilterSpec, which makes me nervous, because I don’t want to invest in a lot of custom code tied to some transient internal class that might disappear by the next release.

  • 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-26T00:32:55+00:00Added an answer on May 26, 2026 at 12:32 am

    I ended up using a mixture of the top solution here, along with this snippet.

    However, I had to tweak the snippet slightly, dropping the field type restriction and adding the new field_path, recently added in 1.3.

    from django.contrib.admin.filterspecs import FilterSpec
    from django.db import models
    from django.utils.safestring import mark_safe
    from django.utils.translation import ugettext as _
    
    class NullFilterSpec(FilterSpec):
        #fields = (models.CharField, models.IntegerField, models.FileField)
    
        @classmethod
        def test(cls, field):
            #return field.null and isinstance(field, cls.fields) and not field._choices
            return field.null and not field._choices
        #test = classmethod(test)
    
        def __init__(self, f, request, params, model, model_admin, field_path=None):
            super(NullFilterSpec, self).__init__(f, request, params, model, model_admin, field_path)
            self.lookup_kwarg = '%s__isnull' % f.name
            self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    
        def choices(self, cl):
            # bool(v) must be False for IS NOT NULL and True for IS NULL, but can only be a string
            for k, v in ((_('All'), None), (_('Has value'), ''), (_('Omitted'), '1')):
                yield {
                    'selected' : self.lookup_val == v,
                    'query_string' : cl.get_query_string({self.lookup_kwarg : v}),
                    'display' : k
                }
    
    # Here, we insert the new FilterSpec at the first position, to be sure
    # it gets picked up before any other
    FilterSpec.filter_specs.insert(0,
        # If the field has a `profilecountry_filter` attribute set to True
        # the this FilterSpec will be used
        (lambda f: getattr(f, 'isnull_filter', False), NullFilterSpec)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a model that looks like: class StockRequest(models.Model): amount_requested = models.PositiveIntegerField(null=True) amount_approved
I have some simple Django Models like this: class Event(models.Model): # some stuff class
I have two simple tables in Django which looks like: class Session(models.Model): id =
Django newbie here. I have two simple models like this: class Fluff(models.Model): # ....
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a simple one-to-many (models.ForeignKey) relationship between two of my model classes: class
I have a Django Model that implements a time range, like this: class Period(models.Model):
I have two simple models in my Django app. Here's what they look like:
I have two very simple classes Submission and Store class Submission(models.Model): title = models.CharField(max_length=50,
I have a simple form in Django that looks like this: class SettingForm(forms.Form): theme

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.