Question

I am setting up a django multisite, they are blog sites, there is a master site and lots of other child sites.

So I have different settings files for different sites, each settings imports the master settings and overrides specific values, a example of the child site settings looks like this:

import os
from os.path import join, dirname

PROJECT_ROOT = dirname(dirname(__file__))
VIRTUALENV_ROOT = dirname(PROJECT_ROOT)

try:
    from settings import *
except: pass

SITE_ID = 3 
SITE_NAME = 'child1'

TEMPLATE_DIRS = ( 
    join(PROJECT_ROOT, 'templates', SITE_NAME),
    join(PROJECT_ROOT, 'templates', 'master'),
    join(PROJECT_ROOT, 'templates'),
)

STATIC_ROOT = join(VIRTUALENV_ROOT, 'public-www', 'static', SITE_NAME)

STATIC_URL = '/static/{}/'.format(SITE_NAME)

STATICFILES_DIRS = ( 
    join(PROJECT_ROOT,'static', SITE_NAME),
    join(PROJECT_ROOT,'static'),
)

HAYSTACK_CONNECTIONS = {.
    'default': {
        'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
        'PATH': os.path.join(VIRTUALENV_ROOT, 'whoosh_index', SITE_NAME),
    },..
}

If I rebuild indexes manually for each site, they all working fine. Here is the search_indexes:

import datetime
from haystack import indexes
from articles.models import Story
from django.contrib.sites.models import Site

current_site = Site.objects.get_current()

class StoryIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')
    introtext = indexes.CharField(model_attr='introtext')
    content = indexes.CharField(model_attr='content')
    author = indexes.CharField(model_attr='author')

    def get_model(self):
        return Story

    def index_queryset(self, using=None):
        return Story.active_objects.filter(sites__in=[current_site])

However, I have the requirement to create the articles on master site, and select which sites the articles to appear on.

The problem is when creating the articles on the master site, it's only updating the master index, but not children sites' indexes. Is there a clever way to do it?

Was it helpful?

Solution

The current work around that works for me, is using single search index, with multivalue field for sites selection, eg:

sites = MultiValueField()

def prepare_sites(self, obj):
    return [site.domain for site in obj.sites.all()]

Then write my own search query:

from haystack.query import SearchQuerySet
from django.contrib.sites.models import Site
from django.shortcuts import render

current_site = Site.objects.get_current()

def search(request):
    cx = {}
    query = request.GET.get('q', '')
    cx['query'] = query
    cx['result'] = SearchQuerySet().filter(content=query, sites__in=[current_site.domain])
    return render(request, 'search/search.html', cx)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top