Monish K Nair
Monish K Nair

Reputation: 136

Django filtering with kwargs

I have the following code.

def alpha(**kwargs):
    q_obj_list = [Q(str(i), kwargs.get(i)) for i in kwargs.keys()]
    reduce(operator.and_, q_obj_list)
    return q_obj_list

q = Elements.objects.all()
q = q.filter(alpha(id=1, is_active=False))

For this code I am receiving an error saying TypeError: 'bool' object has no attribute 'getitem'. I was intending to replace the below code with this.

q = Elements.objects.all()
id = kwargs.get("id")
active = kwargs.get("is_active")
q.filter(id=id,is_active=active)

How to I fix such an error ?

Upvotes: 0

Views: 1564

Answers (1)

Roman Mindlin
Roman Mindlin

Reputation: 842

You should use: q = Elements.objects.filter(**kwargs)

Upvotes: 1

Related Questions