Reputation: 4194
I have a problem when I want to search my queryset with multiple conditional. In my database, the hscode saved as "0105.14.90"
. I need to search the hscodes with following this query "01051490"
. eg:
>>> query = '0105.14.90'
>>> HSCode.objects.filter(hscode=query)
<QuerySet [<HSCode: 0105.14.90>]>
>>> query = '01051490'
>>> HSCode.objects.filter(hscode=query)
<QuerySet []>
The bad think I can do, is look like this:
hscodes = []
query = '01051490'
for hscode in HSCode.objects.order_by('-id'):
if query.isdigit() and hscode.hscode == query:
hscodes.append(hscode)
elif hscode.hscode.replace('.', '') == query:
hscodes.append(hscode)
How can handle it with ORM only?
>>> query = '01051490'
>>> HSCode.objects.filter(Q(hscode=query) | Q(???))
<QuerySet [<HSCode: 0105.14.90>]>
Upvotes: 0
Views: 60