python - 如何使用 django 在多模型中进行搜索

标签 python django python-3.x django-models django-rest-framework

我想从数据库中的所有模型中进行搜索,所以我创建了这个搜索函数来执行此操作...但我总是得到“此搜索没有结果” 如何解决这个问题?

查看.py:

def search(request):
if request.method == 'GET':
    query= request.GET.get('q')
    submitbutton= request.GET.get('submit')


    if query is not None:

        home_database= Homepage.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
        pcprograms_database= PCprogram.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
        androidapk_database= AndroidApks.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
        androidgames_database= AndroidGames.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
        antiruvs_database= Antivirus.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
        systems_database= OpratingSystems.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
        pcgames_database= PCgames.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)

        results= list(chain(home_database,pcprograms_database,androidapk_database,androidgames_database,antiruvs_database,systems_database,pcgames_database))



        context={'results': results,
                 'submitbutton': submitbutton}

        return render(request, 'html_file/enterface.html', context)

    else:
        return render(request, 'html_file/enterface.html')

else:
    return render(request, 'html_file/enterface.html')

html 搜索表单:

   <form id="search_form" action="{% url 'search' %}" method="GET" value="{{request.GET.q}}">
    <input id="search_box" type="text" name="q" value="{{request.GET.q}}"
              placeholder=" Search For ... "/>
    <input id="search_button" type="submit" name="submit" value="Search"/>
</form>

html 文件:

     {% if submitbutton == 'Search' and request.GET.q != '' %}
 {% if results %}
 <h1> <small> Results for </small><b>{{ request.GET.q }}</b> : </h1>
 <br/><br/>

 {% for result in results %}
 <label id="label_main_app"> <img id="img_main_app_first_screen" src="{{result.app_image.url}}" alt="no image found !" height="170" width="165" > {{result.name}} <br><br> <p id="p_size_first_page"> {{result.app_contect}} <br> <br> <a href="{{ result.page_url }}" type="button" class="btn btn-primary"><big> See More & Download </big>  </a> </p>
  </label>


 {% endfor %}
 {% else %}
 <h3> No results for this search </h3>
 {% endif %}
 {% endif %}

请问有什么帮助吗?

最佳答案

如果您尝试使用多个模型字段进行过滤,则需要来自 django.db.modelsQ

Homepage.objects.filter(Q(name__icontains=query)&Q(app_contect__icontains=query),....

关于python - 如何使用 django 在多模型中进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58608635/

相关文章:

python - 使用 mod_wsgi 记录 flask 错误

python - django - 在保存之前比较新旧字段值

django - 何时使用 Haystack/ElasticSearch 与 Django 的 ORM

python - 有时 PyDev 无法将 .py 文件识别为 python 源文件

python - 可选的命令行参数

django - PyCharm 不解析 Django 项目中的模板、模板标签或静态

python-3.x - 以下目录中的cx_freeze : can't find a usable init. tcl

python - key=dictionary.get 的效率如何?

python-3.x - 为什么 tkinter 输入验证需要调用 register()?

jquery - 使用 jQuery 在 Flask/Jinja2 中具有不同 CSS 值的一类