[Django]-Creating custom list filter in django

1👍

Django is too magical sometimes. There is almost certainly a right way to do this, and what I’m doing is probably not it. But I modified my init function to delete my special parameters from the dictionary; and thus the django admin ignored it.

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

    for param_key in self.parameters: 
        if param_key in params.keys():
            self.parameter_values[param_key] = params[param_key]
            del params[param_key]  # This is the anti-magic line
        else:
            self.parameter_values[param_key] = ''

    self.lookup_choices = list(self.lookups())
👤mcneo

-1👍

I’ve seen that happen indeed: it seems you need to leave a “clean” set of parameters to the view.
I’ve seen pop() is used as a more efficient alternative:

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

    for param_key in self.parameters:
        self.parameter_values[param_key] = params.pop(param_key,'')

    self.lookup_choices = list(self.lookups())
👤lai

Leave a comment