【问题标题】:How do I search 2 tables in如何搜索 2 个表
【发布时间】:2022-10-09 00:51:02
【问题描述】:

作为一个新手,我将继续学习 Django .....我想要一些与 1 个搜索查询相关的方向,该查询针对两个包含相同标题(例如客户姓名)的表。 所以表 1 是 2022 年的客户名称,表 2 是 2021 年的客户名称。

我可以创建模型/管理员和 URL 并设置项目。

如何创建查询以同时搜索两个表并显示结果?

View.py
    def index(request,):

    q = request.GET.get('q')

    if q:
        #this is what is searched against.....ie the columns in our model.py
        vector = SearchVector('name')
        
        #This is the value the user is searching for:
        query = SearchQuery (q)
        
        # customers = customer.objects.filter(name__search=q)
        # customers = customer.objects.annotate(search=vector).filter(search=query)
        customers = customer1.objects.annotate(rank=SearchRank(vector, query)).filter(rank__gte=0.001).order_by('-rank')
        
    else:
        customers = None
    
    
    context = {'customers': customers}
    return render(request, 'index.html', context)



Index.html
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>GAEN</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
</head>

<body>

  {% include 'navbar.html' %}

  <div class="container">


    
    {% block content %}

    <br><br><br><br>
    <form>
      <div class="mb-3">
        <label for="Search Query" class="form-label ">
          <h3>db Search Query</h3>
        </label>
        <br><br>
        <input type="text" class="form-control" aria-describedby="#" name="q">
        <br><br>
        <button type="submit" class="btn btn-primary ">Submit</button>
        <button type="submit" class="btn btn-danger">Reset</button>
      </div>
    </form>

    {% if customers %}


    <br>
    <h3><mark>Results:</mark> {{ customer | length }}</h3>

    <br><br>
    {% for customer in customers %}

    <table class="table table-striped">
      <thead>
        <tr>
          <th scope="col">Search Result</th>
          <th scope="col">name</th>                  
        </tr>
      </thead>

        <tbody>
          <tr>
            <th scope="row">1</th>
            <td>{{ customer.name }}</td>            
          </tr>
        </tbody>
      </table>

    
      Rank: {{ customer.rank }}
      <br><br><br>

          {% endfor %}
      
      

      {% endif %}
  {% endblock %}

</div>





  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2"
    crossorigin="anonymous"></script>
   

【问题讨论】:

  • 你必须写你的models.py
  • models.py 只是 2 个类,名称为每个表的模型字段。

标签: django django-models django-views django-forms django-templates


【解决方案1】:

Django-filter 可以轻松搞定!

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    相关资源
    最近更新 更多