Search code examples
djangodjango-rest-frameworkdjango-viewsdjango-querysetdrf-queryset

filter queryset for multiple models in Django


  • I'm implementing a search feature where I'm matching keys from the description. and also matching media if description and media type of ['mp4','mkv','mov','avi'] match so the condition is satisfied.

  • So I have tried many methods but didn't find an efficient way. to make it possible without for loop.

  • I want to use those together.

    • description and media type ['mp4','mkv','mov','avi']
    postinlang_queryset = PostInLanguages.objects.filter(description__contains=search_name)
    media_type_query_set = LanguageMedia.objects.filter(content_type__contains ['mp4','mkv','mov','avi'])
    

Solution

  • yes, it's possible without for loop.Just follow the following script:

    postinlang_queryset = PostInLanguages.objects.filter(description__contains=search_name)
    media_type_query_set = LanguageMedia.objects.filter(content_type__in=['mp4','mkv','mov','avi'])
    

    N.B: content_type__in=['mp4','mkv','mov','avi'] If we pass an empty list then it will never throw exceptions but return empty queryset