Solr Haystack中的Django外键索引

时间:2013-10-08 17:03:54

标签: django solr django-haystack

我正在尝试索引具有一对多(外键)关系的模型。我无法处理如何为此声明search_indexes.py,任何人都知道该怎么做:

models.py:

class CarModel(models.Model):
    manufacturer = models.ForeignKey(Manufacturer)

class Manufacturer(models.Model):
    name = models.CharField(max_length=200)

search_indexes.py:

class ManufacturerIndex(indexes.SearchIndex, indexes.Indexable):
    carmodel = MultiValueField(indexed=True, stored=True, faceted=True)

    def prepare_carmodel(self, obj):
        return ???????????????????    

    def get_model(self):
        return Manufacturer

1 个答案:

答案 0 :(得分:2)

您拥有Manufacturer个实例,因此您需要撤消ForeignKey关系以创建要保存在MultiValueField

中的pks列表(在本例中)
def prepare_carmodel(self, obj):
    return [int(cm.pk) for cm in obj.carmodel_set.all()]