本文介绍了Django haystack + whoosh错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 django应用程序中进行搜索,然后我使用
但我遇到一些麻烦。首先当我尝试rebuild_index或update_index它给我这个错误在下面,还有第二个是当我打字并搜索它给我0个结果。所以我只是想,如果这个rebuild_index固定的搜索问题将被解决。并且请任何人帮助我这个

I'm trying to make a search in my django app then i used haystack and whooshbut i faced some troubles. first when i tried to rebuild_index or update_index it gives me this error right down below, also second one is when i typed and search it gave me 0 results. So i just thought that if this rebuild_index fixed the search problem will be solved. And please anyone help me with this

/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py:903: RuntimeWarning: DateTimeField Subtitle.pub_date received a naive datetime (2014-03-27 16:36:44.341555) while time zone support is active.
  RuntimeWarning)

Indexing 14 Subtitles
ERROR:root:Error updating sub using default
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/update_index.py", line 221, in handle_label
    self.update_backend(label, using)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/update_index.py", line 267, in update_backend
    do_update(backend, index, qs, start, end, total, self.verbosity)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/update_index.py", line 89, in do_update
    backend.update(index, current_qs)
  File "/usr/local/lib/python2.7/dist-packages/haystack/backends/whoosh_backend.py", line 179, in update
    doc = index.full_prepare(obj)
  File "/usr/local/lib/python2.7/dist-packages/haystack/indexes.py", line 204, in full_prepare
    self.prepared_data = self.prepare(obj)
  File "/usr/local/lib/python2.7/dist-packages/haystack/indexes.py", line 195, in prepare
    self.prepared_data[field.index_fieldname] = field.prepare(obj)
  File "/usr/local/lib/python2.7/dist-packages/haystack/fields.py", line 99, in prepare
    raise SearchFieldError("The model '%s' has an empty model_attr '%s' and doesn't allow a default or null value." % (repr(obj), attr))
SearchFieldError: The model '<Subtitle: 3 idiots>' has an empty model_attr 'pub_date' and doesn't allow a default or null value.
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/rebuild_index.py", line 16, in handle
    call_command('update_index', **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 159, in call_command
    return klass.execute(*args, **defaults)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/update_index.py", line 195, in handle
    return super(Command, self).handle(*items, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 385, in handle
    label_output = self.handle_label(label, **options)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/update_index.py", line 221, in handle_label
    self.update_backend(label, using)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/update_index.py", line 267, in update_backend
    do_update(backend, index, qs, start, end, total, self.verbosity)
  File "/usr/local/lib/python2.7/dist-packages/haystack/management/commands/update_index.py", line 89, in do_update
    backend.update(index, current_qs)
  File "/usr/local/lib/python2.7/dist-packages/haystack/backends/whoosh_backend.py", line 179, in update
    doc = index.full_prepare(obj)
  File "/usr/local/lib/python2.7/dist-packages/haystack/indexes.py", line 204, in full_prepare
    self.prepared_data = self.prepare(obj)
  File "/usr/local/lib/python2.7/dist-packages/haystack/indexes.py", line 195, in prepare
    self.prepared_data[field.index_fieldname] = field.prepare(obj)
  File "/usr/local/lib/python2.7/dist-packages/haystack/fields.py", line 99, in prepare
    raise SearchFieldError("The model '%s' has an empty model_attr '%s' and doesn't allow a default or null value." % (repr(obj), attr))
haystack.exceptions.SearchFieldError: The model '<Subtitle: 3 idiots>' has an empty model_attr 'pub_date' and doesn't allow a default or null value.



我的models.py是



and my models.py is

class Movies(models.Model):
    name = models.CharField(max_length=100)
    poster = models.FileField(upload_to="posters/")
    date = models.CharField(max_length=4)
    director = models.CharField(max_length=30, blank=True, null=True)
    actors = models.CharField(max_length=360, blank=True, null=True)
    music = models.CharField(max_length=200, blank=True, null=True)
    company = models.CharField(max_length=200, blank=True, null=True)

    def __unicode__(self):
        return unicode(self.name)

    class Meta:
        verbose_name_plural = 'Movies'


class Subtitle(models.Model):
    name=models.ForeignKey(Movies, related_name="subtitles")
    extension=models.CharField(max_length=5, choices=sub_choices)
    upload=models.FileField(upload_to="subtitles/%Y/%m/%d/")
    user=models.ForeignKey(User)
    pub_date=models.DateTimeField('published', default='')
    def __unicode__(self):
        return unicode(self.name)

    class Meta:
        verbose_name_plural = 'Subtitles'



我的views.py是



my views.py is

def Subtitle_search(request):
    form = SubtitlesSearchForm(request.GET)
    results = form.search()

    return render(request, 'search/search.html', {
        'search_query':search_query,
        'subtitles':results,
        })

在应用文件夹中的search_index.py / *是否放置在我的应用文件夹中? * /

search_index.py in app folder/* is it right to placed in my app folder? */

class SubtitleIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    name = indexes.CharField(model_attr='user')
    pub_date = indexes.DateTimeField(model_attr='pub_date')

    def get_model(self):
        return Subtitle

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())



and forms.py



and forms.py

class SubtitlesSearchForm(SearchForm):

    def no_query_found(self):
        return self.searchqueryset.all()

    def search(self):
        # First, store the SearchQuerySet received from other processing. (the main work is run internally by Haystack here).
        sqs = super(SubtitleSearchForm, self).search()

        # if something goes wrong
        if not self.is_valid():
            return self.no_query_found()

        # you can then adjust the search results and ask for instance to order the results by title
        sqs = sqs.order_by(name)

        return sqs



templates / search / search.html



templates/search/search.html

{% extends 'index.html' %}

{% block content %}
<div class="row">
    <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 class="results">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>
    </div>
{% endblock %}



最终模板/搜索/索引/子/ subtitle_text.txt



eventually templates/search/indexes/sub/subtitle_text.txt

{{ object.name }}


推荐答案

您的错误是:

haystack.exceptions.SearchFieldError: The model '<Subtitle: 3 idiots>' has an empty model_attr 'pub_date' and doesn't allow a default or null value.

Haystack表示, pub_date 字段是对于特定的模型,为null,但是您尚未指定 pub_date 字段可以为null。

Haystack is saying that the pub_date field is null for a particular model, but you haven't specified that the pub_date field can be null.

您可以解决这个问题,通过告诉干草堆,pub_date字段可以为null,通过设置null = True,如下所示:

You can fix this by telling haystack that the pub_date field can be null, by setting null=True, as below:

class SubtitleIndex(indexes.SearchIndex, indexes.Indexable):
    ...
    pub_date = indexes.DateTimeField(model_attr='pub_date', null=True)

这篇关于Django haystack + whoosh错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 10:07