django-haystack 找不到索引 post

django-haystack can not find indexed post

您好,我在 macOS

中使用 elasticsearch@1.7 + django-haystack

我正在使用 postgrepql

制作一个练习网站

用户模型

from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
    nickname = models.CharField(
        max_length=20,
        null=True,
        blank=True,
        )
    phone = models.CharField(
        max_length=20,
        null=True,
        blank=True,
        )
    certification_code = models.CharField(
        max_length=4,
        null=True,
        blank=True,
        )
    passed_certification = models.BooleanField(
        default=False,
        )

    class Meta:
        verbose_name = '유저'
        verbose_name_plural = verbose_name

ItemPost 模型

from django.db import models
from django.conf import settings
from django.utils import timezone

def localtime():
    return timezone.localtime(timezone.now())

class ItemPost(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        )

    # it will used item admin
    def user_nickname_def(self):
        return self.user.nickname

    user_nickname = property(user_nickname_def)

    title = models.TextField()
    created_at = models.DateTimeField(
        default=localtime
        )
    updated_at = models.DateTimeField(
        default=localtime,
        )
    is_deleted = models.BooleanField(
        default=False,
        )

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = '포스트'
        verbose_name_plural = verbose_name

BookList 模型

from django.db import models
from .item_post import ItemPost


class BookList(models.Model):
    # post + booknumber => will used primary key

    post = models.ForeignKey(
        "ItemPost",
        )

    booknumber = models.IntegerField(
        )

    bookname = models.CharField(
        max_length=200,
        )

    bookprice = models.CharField(
        max_length=200,
        )

    is_soldout = models.BooleanField(
        default=False,
        )

    def __str__(self):
        return self.bookname

我想要索引 ItemPost 并且当客户搜索书名(或某个词)时

posts 将显示我的网页

这是我的 search_indexes、itempost_text.txt 和模板

search_indexes

from haystack import indexes
from items.models import ItemPost
import datetime

class ItemPostIndex(indexes.SearchIndex, indexes.Indexable):

    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')
    updated_at = indexes.DateTimeField(model_attr='updated_at')

    def get_model(self):
        return ItemPost

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(updated_at__lte=datetime.datetime.now())

itempost_text.txt

{{ objects.title }}
{{ objects.user.get_full_name }}
{{ objects.body }}

search.html

{% extends "base.html" %}

{% block title %}남서울 도서공유{% endblock %}

{% block body %}
<div class="container">
    <section class="col-md-2"></section>
    <section class="col-md-8">

  <h2>Search</h2>

    <form method="get" action=".">
        <table>
            {{ form.as_table }}
            <tr>
                <td>&nbsp;</td>
                <td>
                    <input type="submit" value="Search">
                </td>
            </tr>
        </table>

    {% if query %}
        <h3>Results</h3>

        {% for result in page.object_list %}
            <p>
                <a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
            </p>
        {% empty %}
            <p>No results found.</p>
        {% endfor %}

        {% if page.has_previous or page.has_next %}
            <div>
                {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
                |
                {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
            </div>
        {% endif %}
    {% else %}
        {# Show some example queries to run, maybe query syntax, something else? #}
    {% endif %}
</form>

</section>
<section class="col-md-2"></section>
</div>

{% endblock %}

我觉得一切都还好

当我命令时

python ./manage.py rebuild_index

WARNING: This will irreparably remove EVERYTHING from your search index in connection 'default'.
Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.
Are you sure you wish to continue? [y/N] y
Removing all documents from your index because you said so.
All documents removed.
/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/db/models/fields/__init__.py:1453: RuntimeWarning: DateTimeField ItemPost.updated_at received a naive datetime (2016-12-23 15:47:04.759121) while time zone support is active.
  RuntimeWarning)

Indexing 2 포스트

(GET /haystack/_mapping)<- 没有出现

2 个对象

In [1]: ItemPost.objects.all()
Out[1]: [<ItemPost: Java book>, <ItemPost: python>]

但是当我使用关键字 'Java' 或 'python'

搜索 post 时

任何post都没有出现

我认为是django-haystack的问题,不是elasticsearch的问题 但我不知道为什么

请有人帮助我

f**king 可怕的错误

itempost_text.txt

{{ object.title }}
{{ object.user.get_full_name }}
{{ object.body }}

没有

itempost_text.txt

{{ object's'.title }}
{{ object's'.user.get_full_name }}
{{ object's'.body }}