Friday, July 19, 2013

Re: field-null or not-null filter?

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+unsubscribe@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.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate