2

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

模型.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
4

1 回答 1

2

您有Manufacturer实例,因此您需要反转ForeignKey关系以创建要保存在MultiValueField

def prepare_carmodel(self, obj):
    return [int(cm.pk) for cm in obj.carmodel_set.all()]
于 2013-10-08T17:39:44.923 回答