# Authors: Guglielmo Celata <guglielmo.celata at gmail.com># # File: <your project>/filters.pyfromdjango.dbimportmodelsfromdjango.utils.encodingimportsmart_unicodefromdjango.utils.translationimportugettextas_fromdjango.contrib.admin.filterspecsimportFilterSpecclassIsLinkedFilterSpec(FilterSpec):""" Adds filtering by existence of at least one related object. Note: this is a temporary hack. Django 1.4 implements a proper mechanism to define custom filters * Add this code in filters.py * Import the class in your models * set `my_model_field.is_linked_filter = True` for the given field in the class specification * add 'my_model_field' to the **list_filter** tuple in admin.py """def__init__(self,f,request,params,model,model_admin,field_path=None):super(IsLinkedFilterSpec,self).__init__(f,request,params,model,model_admin,field_path)self.lookup_kwarg='%s__isnull'%self.field_pathself.lookup_val=request.GET.get(self.lookup_kwarg,None)defchoices(self,cl):yield{'selected':self.lookup_val==None,'query_string':cl.get_query_string({},[self.lookup_kwarg]),'display':_('Any')}yield{'selected':self.lookup_val=='False','query_string':cl.get_query_string({self.lookup_kwarg:False}),'display':_('Yes')}yield{'selected':self.lookup_val=='True','query_string':cl.get_query_string({self.lookup_kwarg:True}),'display':_('No')}deftitle(self):return"Linked"# registering the filterFilterSpec.filter_specs.insert(0,(lambdaf:getattr(f,'is_linked_filter',False),IsLinkedFilterSpec))