我想添加一个字段从第二个模型到django-haystack查询。我有两个模型具有以下结构:用django haystack查询第二个模型
class Product(models.Model):
created_date = models.DateField(auto_now_add=True)
name = models.CharField(max_length=254)
summary = models.CharField(null=True, blank=True, max_length=255)
....
class Color(models.Model):
product_color = models.CharField(max_length=256, blank=True, null=True)
created_date = models.DateField(auto_now_add=True)
slug = models.SlugField(max_length=254)
product = models.ForeignKey('Product')
我有以下search_index.py:
from django.utils import timezone
from haystack import indexes
from .models import Product
from .models import Color
class ProductIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return Product
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(
created_date__lte=timezone.now())
我如何可以添加Color
模型product_color
到搜索索引,这样,如果有人包括在搜索查询中product_color
的部分部分将返回与颜色具有ForeignKey关系的Product
?
:SearchIndex Api
例如。对不起,我错过了这一点。感谢您的帮助Aamir! – ajt