Created
May 5, 2013 20:08
-
-
Save kezabelle/5522019 to your computer and use it in GitHub Desktop.
Haystack indexing support for both 1.2 style, and the new 2.0 style classes.
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
import logging | |
from haystack.fields import CharField # Whatever fields you want ... | |
from myapp.models import MyModel | |
logger = logging.getLogger(__name__) | |
try: | |
# haystack 1.2x | |
from haystack import site | |
from haystack.indexes import SearchIndex | |
class Indexable(object): pass | |
logger.info('Haystack 1.x found') | |
except ImportError: | |
# haystack 2.0 | |
site = None | |
from haystack.indexes import SearchIndex, Indexable | |
logger.info('Haystack 2.x found') | |
class MyIndex(SearchIndex, Indexable): | |
url = CharField(model_attr='get_absolute_url') # example, using the fields we imported. | |
def get_model(self): | |
""" 2.X style """ | |
if not hasattr(self, 'model'): | |
self.model = MyModel | |
return self.model | |
def index_queryset(self, using=None): | |
""" calling get_model makes us safe on both versions""" | |
return self.get_model().objects.all() | |
if site is not None: | |
#: 1.2.X style | |
site.register(MyModel, MyIndex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment