I am creating an search and filter system in which user can type and search related courses and institutions. This is my model structure:
Model Structure:
Institutions:
- Name
- Id
- email
- address
...... etc.
Courses:
- owner = models.ForeignKey(Institution, on_delete=models.CASCADE)
- name
- ref
- license
- overview
..... etc
Now by using the above, i have created a search bar in which user needs to type something and then press "search" button or else press "Enter" key. This will show search results. My search code:
query = request.GET.get('q')
courses = Course.objects.filter(name__icontains=query)
institution= Institution.objects.filter(name__icontains=query)
queryset_chain = chain(courses,institution)
So the problem here is that, I want to display the result as follows.
scenario 1:
Lets say that course and Institution has same name like "tech".
When user search for "tech", I need to show result as follows:
Institution Name
- Course name
scenario 2:
Lets say that course has name as "tech" but not Institution has that name.
When user search for "tech", I need to show result as follows: In this case, I need to show the Course name and also correspondant Institution name:
Institution Name
- Course name
scenario 3:
where Institution has "tech" as name. But no course name.
Institution Name
- any random Course name
How to handle this kind of clubbed search and filter ?