Search code examples
djangodjango-admindjango-admin-filters

Django admin queryset filtering by foreign key backwards relation


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.


Solution

  • 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))