[Answered ]-Objects.filter() and Foreign Key NULL

2👍

You could write it like this:

query = {
    'name__icontains': request.POST.get('student_name',''),
}
school_name = request.POST.get('school_name', None)
if school_name is not None:
    query['school__name__icontains'] = school_name
results = student.objects.filter(**query)

And similarly for the other fields. This would mean one if statement for each field. You can even do this dynamically as the keys in query are just strings. That means you can compute them as well and add them to query when needed.

Leave a comment