Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:在多个模型中搜索_Python_Django_Qobject - Fatal编程技术网

Python Django:在多个模型中搜索

Python Django:在多个模型中搜索,python,django,qobject,Python,Django,Qobject,我想在django中实现一个多模型搜索,这意味着我想在两个模型中搜索不同的字段 我的两个模型是“故事”和“地点”: 我的视图合并了两个查询 from django.shortcuts import render, get_object_or_404, redirect from django.db.models import Q from django.views.generic import (ListView) from django.contrib.auth.models import U

我想在django中实现一个多模型搜索,这意味着我想在两个模型中搜索不同的字段

我的两个模型是“故事”和“地点”:

我的视图合并了两个查询

from django.shortcuts import render, get_object_or_404, redirect
from django.db.models import Q
from django.views.generic import (ListView)
from django.contrib.auth.models import User
from places.models import Place
from .models import Story


class SearchResultsView(ListView):#
    template_name = 'story/results.html'

    def get_queryset(self):
        request = self.request
        query = request.GET.get('q', None)

        if query is not None:
            story_list = Story.objects.filter(Q(title__icontains=query))
            place_list = Place.objects.filter(Q(name__icontains=query))

        results = chain(story_list, place_list)

        return results

最后,我在模板中显示搜索:

{% extends "story/base.html" %}
{% block content %}
{% load static %}

<form class="form-inline my-2 my-lg-0" method="get">
                  <input name="q" class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                  <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>

<h1>Search Results Stories</h1>
<div class="row">
<div class="col-md-8 narrow "> 

  {% for story in object_list %}
        <article class="media content-section">
          <img class="rounded-circle article-img" src="{{ story.author.profile.image.url }}">
      <div class="media-body">
        <div class="article-metadata">
          <a class="mr-2" href="{% url 'user-stories' story.author.username %}">{{ story.author }}</a>
          <small class="text-muted">{{ story.date_posted|date:"F d, Y" }}</small>

        </div>
        <h2><a class="article-title" href="{% url 'story-detail' story.id %}">{{ story.title }}</a></h2>
        <p class="article-content">{{ story.content }}</p>
        <audio
        controls
        src="{{ story.audio.url }}">
            Your browser does not support the
            <code>audio</code> element.
      </audio>
      <small class="text-muted">{{ story.Place }}, {{ story.Place.city }}</small>

      </div>
    </article>
  {% endfor %}

  <h1>Search Results Places</h1>

      {% for place in place %}
        <article class="media content-section">
          <img class="rounded-circle article-img" src="{{place.location_image.url}}">
           <div class="media-body">
            <div class="article-metadata">
            <a class="mr-2">{{ place.city }}</a>
            <small class="text-muted">Germany</small> 
          </div>
            <h2><a class="article-title" href="{% url 'place-detail' place.id %}">{{ place.name }}</a></h2>
            <p class="article-content">{{ place.description }}</p>       
         </div>
    </article>
      {% endfor %}

  </div>
  </div>


{% endblock content%}

当我键入“Town”时,我会得到:

  • 故事模型中的“故事标题城镇故事标题”(标题)

  • 来自地名模型(名称)的“地名城镇地名”


  • 不知何故,只有“标题”模型中的搜索起作用,但“位置”中的搜索结果不会显示。

    object\u list
    返回的是
    get\u queryset
    返回的内容,但您认为这里有什么-“{%for place in place%}”?首先,它不是一个现有变量,第二件事是,您只是在迭代过程中重新定义它。如果您将位置与对象列表中的故事链接在一起,您希望如何迭代位置
    {% extends "story/base.html" %}
    {% block content %}
    {% load static %}
    
    <form class="form-inline my-2 my-lg-0" method="get">
                      <input name="q" class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
    
    <h1>Search Results Stories</h1>
    <div class="row">
    <div class="col-md-8 narrow "> 
    
      {% for story in object_list %}
            <article class="media content-section">
              <img class="rounded-circle article-img" src="{{ story.author.profile.image.url }}">
          <div class="media-body">
            <div class="article-metadata">
              <a class="mr-2" href="{% url 'user-stories' story.author.username %}">{{ story.author }}</a>
              <small class="text-muted">{{ story.date_posted|date:"F d, Y" }}</small>
    
            </div>
            <h2><a class="article-title" href="{% url 'story-detail' story.id %}">{{ story.title }}</a></h2>
            <p class="article-content">{{ story.content }}</p>
            <audio
            controls
            src="{{ story.audio.url }}">
                Your browser does not support the
                <code>audio</code> element.
          </audio>
          <small class="text-muted">{{ story.Place }}, {{ story.Place.city }}</small>
    
          </div>
        </article>
      {% endfor %}
    
      <h1>Search Results Places</h1>
    
          {% for place in place %}
            <article class="media content-section">
              <img class="rounded-circle article-img" src="{{place.location_image.url}}">
               <div class="media-body">
                <div class="article-metadata">
                <a class="mr-2">{{ place.city }}</a>
                <small class="text-muted">Germany</small> 
              </div>
                <h2><a class="article-title" href="{% url 'place-detail' place.id %}">{{ place.name }}</a></h2>
                <p class="article-content">{{ place.description }}</p>       
             </div>
        </article>
          {% endfor %}
    
      </div>
      </div>
    
    
    {% endblock content%}