Re: field-null or not-null filter?

2013-07-19 Thread Martin Becker
*You can define your own NullListFilter. I did it this way:*

class NullListFilter(FieldListFilter):
def __init__(self, field, request, params, model, model_admin, 
field_path):

self.lookup_kwarg = '%s__isnull' % field_path
self.lookup_val = request.GET.get(self.lookup_kwarg, None)

super(NullListFilter, self).__init__(field, request, params, model,
 model_admin, field_path)

def expected_parameters(self):
return [self.lookup_kwarg]

def choices(self, cl):
for lookup, title in (
(None, _('All')),
('False', _('Yes')),
('True', _('No'))):
yield {
'selected': self.lookup_val == lookup,
'query_string': cl.get_query_string({
self.lookup_kwarg: lookup, }),
'display': title,
}

FieldListFilter.register(lambda f: True, NullListFilter)

*Then you can use it in admin like this:*

list_filter = [('field_which_is_foreign_key', NullListFilter)]

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




field-null or not-null filter?

2012-06-14 Thread Bram de Jong
Hey guys,


I was wondering if there is a way of making a field null vs not-null filter
without coding it yourself?

I tried:

list_filter = (('field_which_is_foreign_key', BooleanFieldListFilter),)

In the end I wrote this little one:

class MyFieldFilter(SimpleListFilter):
title = _(' field_which_is_foreign_key ')
parameter_name = ' field_which_is_foreign_key _n'

def lookups(self, request, model_admin):
return (
('T', _('Null')),
('F', _('Not Null')),
)

def queryset(self, request, queryset):
if self.value() == 'T':
return queryset.filter( field_which_is_foreign_key=None)
if self.value() == 'F':
return queryset.exclude( field_which_is_foreign_key =None)


But that's not very generic, I might need this for more fields!
Any way to do it with a bool filter?


 - bram

-- 
http://www.samplesumo.com
http://www.freesound.org
http://www.smartelectronix.com
http://www.musicdsp.org

office: +32 (0) 9 335 59 25
mobile: +32 (0) 484 154 730

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.