Created
July 19, 2012 05:15
-
-
Save benspaulding/3140920 to your computer and use it in GitHub Desktop.
Possible fix to get django-faq working with Haystack 1.X-2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/faq/search_indexes.py b/faq/search_indexes.py | |
index 1515984..8032937 100644 | |
--- a/faq/search_indexes.py | |
+++ b/faq/search_indexes.py | |
@@ -1,29 +1,59 @@ | |
# -*- coding: utf-8 -*- | |
from haystack import indexes | |
-from haystack.sites import site | |
from faq.settings import SEARCH_INDEX | |
from faq.models import Topic, Question | |
+# Haystack 2.0 (commit 070d46d72f92) requires that concrete SearchIndex classes | |
+# use the indexes.Indexable mixin. Here we workaround that so our SearchIndex | |
+# classes also work for Haystack 1.X. | |
+try: | |
+ mixin = indexes.Indexable | |
+except AttributeError: | |
+ mixin = object | |
+ | |
+ | |
class FAQIndexBase(SEARCH_INDEX): | |
text = indexes.CharField(document=True, use_template=True) | |
url = indexes.CharField(model_attr='get_absolute_url', indexed=False) | |
-class TopicIndex(FAQIndexBase): | |
+class TopicIndex(FAQIndexBase, mixin): | |
+ # Required method for Haystack 2.0, but harmless on 1.X. | |
+ def get_model(self): | |
+ return Topic | |
+ | |
+ # ``get_queryset`` is deprecated in Haystack v2, and ``index_queryset`` | |
+ # ought to be used instead. But we must use the former to support | |
+ # Haystack < 1.2.4. Support for such older version is likely to be dropped | |
+ # in the near future. | |
def get_queryset(self): | |
return Topic.objects.published() | |
-class QuestionIndex(FAQIndexBase): | |
+class QuestionIndex(FAQIndexBase, mixin): | |
+ | |
+ # Required method for Haystack 2.0, but harmless on 1.X. | |
+ def get_model(self): | |
+ return Question | |
+ # ``get_queryset`` is deprecated in Haystack v2, and ``index_queryset`` | |
+ # ought to be used instead. But we must use the former to support | |
+ # Haystack < 1.2.4. Support for such older version is likely to be dropped | |
+ # in the near future. | |
def get_queryset(self): | |
return Question.objects.published() | |
-site.register(Topic, TopicIndex) | |
-site.register(Question, QuestionIndex) | |
+# try/except in order to register search indexes with site for Haystack 1.X | |
+# without throwing exceptions with Haystack 2.0. | |
+try: | |
+ from haystack.sites import site | |
+ site.register(Topic, TopicIndex) | |
+ site.register(Question, QuestionIndex) | |
+except ImportError: | |
+ pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment