简体   繁体   English

Django 中多个模型的过滤器查询集

[英]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.如果['mp4','mkv','mov','avi']描述和媒体类型匹配,则还匹配媒体,因此满足条件。

  • So I have tried many methods but didn't find an efficient way.所以我尝试了很多方法,但没有找到有效的方法。 to make it possible without for loop.使没有for循环成为可能。

  • I want to use those together.我想一起使用它们。

    • description and media type ['mp4','mkv','mov','avi']描述和媒体类型['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:是的,没有for循环是可能的。只需按照以下脚本操作:

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注意: content_type__in=['mp4','mkv','mov','avi']如果我们传递一个空列表,那么它永远不会抛出异常,而是返回空查询集

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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