Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created May 5, 2013 20:08
Show Gist options
  • Save kezabelle/5522019 to your computer and use it in GitHub Desktop.
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.
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