[Answered ]-Appending filters to django models

2👍

✅

You could obtain all attributes selected by the user and then iterate over:

# sel_att holds the user selected attributes.

result = Person.objects.all()

for att in sel_att:
    result = result.filter(
        attributes__attribute_name=att.attribute_name, 
        attributes__attribute_type=att.attribute_type
    )

0👍

Use the Q module for complex lookups.

For example:

from django.db.models import Q
Person.objects.get(Q(attributes__attribute_name='London') | Q(attributes__attribute_name='History')

Within a QuerySet a | acts as an OR and a , acts as an AND, pretty much as expected.

The problem with chanining filters is you can only implement an AND logic between them, for a complex AND, OR, NOT logic Q would be the better way to go.

Leave a comment