Reputation: 4702
Multiple filters needs to be applied to a QuerySet
. Should I query the database multiple times, or do the sorting/filtering in Python
?
data = Data.objects.all()
for f in somefilters:
tempdata = data.filter(*f.args)
# do work
or
data = list(Data.objects.all())
for f in somefilters:
tempdata = filter(data, key=...)
#do work
Upvotes: 2
Views: 253
Reputation: 55197
Unless you have an absurd number of filters, you should probably just use Q
objects. That's what they're for.
from django.db.models import Q
q = Q()
for filter in filters:
q |= Q(*f.args)
data = Data.objects.filter(q)
# Do things with `data`
Upvotes: 4