Suppose,
A model named Education
contains the fields degree
and field
, and other model Resume
contains the fields skill
and role
.
A third model is Candidates
and has a foreign key relation with the above models.
I want the user to search candidates by their skill
, role
, degree
or field
.
For example: if a query string like {'java','developer','MS','IT'}
is passed, Django should show the all the candidates matching any one of the values in the query string.
I'm using Django Rest Multiple Models to search on multiple models in Django Rest Framework. Make sure to read the docs carefully, especially the section on using viewsets which explains how to set up your endpoints. It seems really well built and documented, and to support everything I expect such as limits and filters.
I don't think there's an automatic way to do this in django. But you can always OR multiple searches together using Q. The basic usage of Q is as follows:
To use multiple queries you can easily build together these statements using a for statement (assuming queries is a list or set of query strings):
Now you'd want to search over multiple models, so you would have to include those joins as well. It's not exactly clear from your question how you'd want to search, but I'd guess you'd like to basically search for candidates and that all keywords need to be matched by the found items. So the query could be done like this:
If you're doing this with the Django Rest Framework (DRF), you will want to use django_filters as referenced by DRF.
To do what you're talking about in my project, I created a generic extension of a
django_filters.Filter
:This is used in a
django_filter.FilterSet
like this:Which is instantiated like:
Finally, a
GET
request like:will return all
SampleSet
's that have all offoo
,de
, andfa
in at least one of the fieldsfield_foo
,bar
, orbaz
.If you specify parameter
token_reducer
to beoperator.or_
, then that query would return allSampleSet
s that have any offoo
,de
, orfa
in at least one of the fieldsfield_foo
,bar
, orbaz
.Finally,
token_prefix
andtoken_suffix
are a way to insert wild cards (substring matching) or other prefixes or suffixes.