7

I have models A and B, where B has a FK to A.

I use django 1.3 and I need two django admin filters:

1) a.b_set.exists() # (True/False)

2) not a.b_set.filter(some_condition=False).exists() # (True/False)

How can I achieve that? Sadly, I couldn't find any solutions by googling.

aemdy
  • 3,702
  • 6
  • 34
  • 49
  • Does `A.objects.filter(b__isnull=False)` and `A.objects.filter(b__isnull=False, some_condition=False)` help? – mkoistinen Nov 30 '12 at 12:31
  • Maybe, but I am asking how could I write a custom display_filter, not how to make a query. – aemdy Nov 30 '12 at 12:44
  • Is upgrading to Django 1.4 an option by any chance? List filters got way more flexible there. You can basically specify your own list filter class: https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter – ptrck Dec 04 '12 at 13:31
  • Well, at this moment the upgrading is not possible, but the flexibility in 1.4 looks nice. – aemdy Dec 04 '12 at 15:30

2 Answers2

5

You need to read this: Custom Filter in Django Admin on Django 1.3 or below

This is my first attempt without any testing, but you should see more or less how its done -

from django.db import models
from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext as _

class BNullSetFilterSpec(FilterSpec):

    def __init__(self, f, request, params, model, model_admin):
        super(BSetFilterSpec, self).__init__(f, request, params, model, model_admin)

        self.links = (
            ('Yes', {'b__isnull': False}),
            ('No', {}))

    def title(self):
        return _('B Set')

# registering the filter
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'empty_bset', False), BNullSetFilterSpec))
Community
  • 1
  • 1
Francis Yaconiello
  • 10,829
  • 2
  • 35
  • 54
1

What @ptrck mentioned is right. Couldn't you switch to 1.4 ?

In this case, as in the doc described, without need to change the lookups, do something like (for the first here and then the second accordingly):

from django.contrib.admin import BooleanFieldListFilter

class BInA(BooleanFieldListFilter):
    def queryset(self, request, queryset):
        if self.value() is True:
            return queryset.filter(a.b_set.exists())
        else:
            return queryset.filter(a.b_set.exists() is False)
Rmatt
  • 1,287
  • 1
  • 16
  • 30