我正在建立一个django多站点,他们是博客站点,有一个主站点和许多其他子站点。
所以我有不同网站的不同设置文件,每个设置都会导入主设置并覆盖特定值,子网站设置的示例如下所示:
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),
},..
}
如果我为每个站点手动重建索引,它们都可以正常工作。这是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])
但是,我需要在主站点上创建文章,并选择要在哪些站点上显示文章。
问题是在主站点上创建文章时,它只更新主索引,而不更新子站点的索引。有一种聪明的方法吗?
答案 0 :(得分:0)
当前适合我的工作是使用单一搜索索引,使用多值字段进行网站选择,例如:
sites = MultiValueField()
def prepare_sites(self, obj):
return [site.domain for site in obj.sites.all()]
然后编写我自己的搜索查询:
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)