问题
I'm trying to make a search in my django app then i used haystack and whoosh but 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
errors:
/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.
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'
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 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
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
{% extends 'index.html' %}
{% block content %}
<div class="row">
<h2>Search</h2>
<form method="get" action=".">
<table>
{{ form.as_table }}
<tr>
<td> </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 }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %}
</div>
{% endif %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</form>
</div>
{% endblock %}
eventually templates/search/indexes/sub/subtitle_text.txt
{{ object.name }}
回答1:
Your error is:
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 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.
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)
来源:https://stackoverflow.com/questions/22694966/django-haystackwhoosh-error