1👍
You can chain calls to filter
within if statements to "stack" conditional filters
tasks = Task.objects.filter(
datetime__contains=date
)
if uid != 0:
tasks = tasks.filter(user=uid)
if mid != 0:
tasks = tasks.filter(member=mid)
if jid != 0:
tasks = tasks.filter(job=jid)
0👍
A simple way might be dictionary unpacking:
data = {'user': uid, 'member': mid, 'job': jid}
tasks = Task.objects.filter(
**{ k: v for k, v in data.items() if v != 0 },
datetime__contains=date
)
Source:stackexchange.com