First we want to return the queryset of JobPostSkillSet objects that fit our search criteria
From that queryset, we then return the queryset of corresponding JobPost objects
In this particular example, let's say we send skills = ['node', 'postgresql'] as query parameters
These skillsets have id values of 4 and 5 respectively.
Notice that our JobPostSkillSet queryset returns instances 1, 5 and 6 even though instance 5 does not contain our skillset parameter.
This is due to the fact that JobPost instance 3 requires skills = ['mysql', 'node'] such that the JobPostSkillSet queryset will return the JobPostSkillSet instance that takes JobPost instance 3 and SkillSet instance 3 as Foreign Keys.
class JobPostView(APIView):
def get(self, request):
# we can receive all query parameter values w/ the same key values
# and store them into a list variable
skills = request.query_params.getlist('skills', '')
# if we wish to make a query using multiple conditions from a list
query = Q()
for skill in skills:
query.add(Q(skill_set__name=skill), Q.OR)
job_skills = JobPostSkillSet.objects.filter(query)
# SELECT * FROM jobpostskillset WHERE (skill_set__name='node') OR (skill_set__name='postgresql'))
job_posts = JobPost.objects.filter(
id__in=[job_skill.job_post.id for job_skill in job_skills]
)
if job_posts.exists():
serializer = JobPostSerializer(job_posts, many=True)
return Response(serializer.data)
Our final output will be serialized data of JobPost instance 1 and 3: