[Fixed]-Django objects.filter with args in loop

1👍

Use **kwargs in your function definition and filter out None valued items using a dictionary comprehension before passing and unpacking the named arguments to filter:

def search(**kwargs):
    kwargs = {k: v for k, v in kwargs.items() if v}
    queryset = Organization.objects.filter(**kwargs)
    return queryset

Leave a comment