Skip to content

Instantly share code, notes, and snippets.

@toastdriven
Created June 26, 2012 16:31
Show Gist options
  • Save toastdriven/2996919 to your computer and use it in GitHub Desktop.
Save toastdriven/2996919 to your computer and use it in GitHub Desktop.
# search_indexes.py
from haystack import indexes
from myapp.models import TheModel
class TheModelSearchIndex(indexes.SearchIndex, indexes.Indexable):
# The usual fields.
# Then...
sites = indexes.MultiValueField()
def get_model(self):
return TheModel
def prepare_sites(self, obj):
return list(obj.sites.all().values_list('pk', flat=True))
# forms.py
from django.contrib.sites.models import Site
from haystack.forms import SearchForm
class SiteBasedSearchForm(SearchForm):
def search(self):
results = super(SiteBasedSearchForm, self).search()
site = Site.objects.get_current()
return results.filter(sites=site.pk)
# urls.py
from django.conf.urls.defaults import patterns, url
from haystack.views import SearchView
from wherever.forms import SiteBasedSearchForm
urlpatterns = patterns('',
# The usual.
# ...Then...
url(r'^search/$', SearchView(form_class=SiteBasedSearchForm))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment