我正在尝试索引具有一对多(外键)关系的模型。我无法处理如何为此声明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
答案 0 :(得分:2)
您拥有Manufacturer
个实例,因此您需要撤消ForeignKey
关系以创建要保存在MultiValueField
def prepare_carmodel(self, obj):
return [int(cm.pk) for cm in obj.carmodel_set.all()]