I am trying to compare my query search to all my model fields, but I can't figure how to do it in more than one field.
this is my code.
expense = Expense.objects.filter(user=request.user.id).order_by('date')
q = request.GET['q']
result = expense.filter(name__icontains=q)
I want to check in: name
, amount
, category
Thanks in advance
From Django documentation:
"Keyword argument queries – in filter(), etc. – are “AND”ed together. If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects."
from django.db.models import Q
expense.objects.filter(
Q(name__icontains=q) | Q(amount__icontains=q) | Q(category__icontains=q)
)
I'm not sure about the type of amount and category in your model so icontains may not work on them.
see this link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With