简体   繁体   中英

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'])

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'])

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM