Created
June 26, 2012 16:31
-
-
Save toastdriven/2996919 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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