Django--Making query use part of the matching name

31 views
Skip to first unread message

shaw...@gmail.com

unread,
Apr 25, 2018, 8:13:40 PM4/25/18
to Django users
Hello everyone!

Currently I am working on Django.

I defined a 'Search' function in views.py:

def search(request):
q = request.GET.get("q")
if q:
ReactResul = Reactionsmeta.objects.filter(id__contains=q)
MetaResul = Metabolites.objects.filter(id__contains=q)
GeneResul = Genes.objects.filter(id__contains=q)

else:
# you may want to return Customer.objects.none() instead
ReactResul= Reactionsmeta.objects.all()
GeneResul = Genes.objects.all()
MetaResul = Metabolites.objects.all()
context = dict(result_1=MetaResul, q=q, result_2=ReactResul, result_3 = GeneResul)
return render(request, "Recon/search.html", context)

And now I want to make it more powerful.

If I want to search '10FTH86RSK' but I cannot remember the whole name, I can just use part of the string to make query. For example, if I type '10FTK', '10FTH86RSK' should be returned as result.
In that case, '10FTK', '10F6TK' or '10FTK4' should also be returned as results.

So how could I achieve this function?

James Farris

unread,
Apr 25, 2018, 9:14:10 PM4/25/18
to django...@googlegroups.com
I believe you want to use icontains rather than contains. It’s case insensitive. 

I’m not sure without using regex it’s possible to return the results based on any character in any order in the search string. 
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/793b8999-cac4-4c2d-94cc-eabb80f4e702%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

shaw...@gmail.com

unread,
Apr 26, 2018, 5:31:58 AM4/26/18
to Django users
Thank you for your help! And yes, I think I should use regex. But could you please show me how to use it in my example, since I am new to django..

在 2018年4月26日星期四 UTC+1上午2:14:10,James Farris写道:

James Farris

unread,
Apr 26, 2018, 11:19:15 AM4/26/18
to django...@googlegroups.com
Try using filter(id__regex=r'\w’)

I’m not sure if this will give you the exact results your looking for, but worth a shot. 


Sent from my mobile device

shaw...@gmail.com

unread,
Apr 26, 2018, 12:02:50 PM4/26/18
to Django users
But how about my query string? I am using 'q' as the search term

在 2018年4月26日星期四 UTC下午3:19:15,James Farris写道:

Anthony Flury

unread,
Apr 28, 2018, 8:24:33 PM4/28/18
to django...@googlegroups.com
What are the rules - you say that a query string of '10FTK' should match
'10FTH86RSK', but also '10FTK', '10F6TK', '10FTK4'

I think the problem is that the rules aren't 100% clear.

* For '10FTK' to match '10FTH....' you actually only care about the
first 4 characters ?
* For '10FTK' to match '10FTK...' implies you care about the first 5
* For '10FTK' to match '10F6TK...' implies you care about your 5
characters with an option numeric inserted after char 3 ?

so - should it match '106FTK ...' or '10TFK' or '10F99TK'

I can suggest a few strategies :

* With English spelling it is not common for a knowledgable to get the
first letter wrong - so you could find that the user will know they
want '10...' and that the 'FTK' bit might be mis-remembered - so
search on the first two characters only.
* Use the entered code to build a more fuzzy search - so 10FTK becomes
something like r'10*F.*T.*(K|H)' (for instance starting with the
characters '10' and looking for FTK or FTH with possible intervening
options.
* Typically long code words are difficult to remember - - is it
possible that '10FTH86RSK' actually represents a concept that can be
categorized and described in English - for instance rather than try
to implement a fuzzy search for the code, you actually provide a
user friendly categorization drill down; so the user works down a
tree of specific natural language descriptions - which internally
builds the code '10FTH86RSK'

> On Apr 25, 2018, at 5:13 PM, shaw...@gmail.com
> <mailto:shaw...@gmail.com> wrote:
>
>> Hello everyone!
>>
>> Currently I am working on Django.
>>
>> I defined a 'Search' function in views.py:
>>
>> def search(request):
>> q = request.GET.get("q")
>> if q:
>> ReactResul = Reactionsmeta.objects.filter(id__contains=q)
>> MetaResul = Metabolites.objects.filter(id__contains=q)
>> GeneResul = Genes.objects.filter(id__contains=q)
>>
>> else:
>> # you may want to return Customer.objects.none() instead ReactResul= Reactionsmeta.objects.all()
>> GeneResul = Genes.objects.all()
>> MetaResul = Metabolites.objects.all()
>> context =dict(result_1=MetaResul, q=q, result_2=ReactResul, result_3 = GeneResul)
>> return render(request, "Recon/search.html", context)
>>
>> And now I want to make it more powerful.
>>
>> If I want to search '10FTH86RSK' but I cannot remember the whole
>> name, I can just use part of the string to make query. For example,
>> if I type '10FTK', '10FTH86RSK' should be returned as result.
>> In that case, '10FTK', '10F6TK' or '10FTK4' should also be returned
>> as results.
>>
>> So how could I achieve this function?
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to django-users...@googlegroups.com
>> <mailto:django-users...@googlegroups.com>.
>> To post to this group, send email to django...@googlegroups.com
>> <mailto:django...@googlegroups.com>.
>> <https://groups.google.com/d/msgid/django-users/793b8999-cac4-4c2d-94cc-eabb80f4e702%40googlegroups.com?utm_medium=email&utm_source=footer>.
>> For more options, visit https://groups.google.com/d/optout.
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/9EC3A76F-EE60-47DC-B4DD-9092D8321EC1%40gmail.com
> <https://groups.google.com/d/msgid/django-users/9EC3A76F-EE60-47DC-B4DD-9092D8321EC1%40gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--
--
Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

Reply all
Reply to author
Forward
0 new messages