如何创建一个抽象的SearchIndex类,类似于Django让您创建抽象基本模型的方法?在
我有几个搜索索引,我想给出相同的基本字段(object_id、时间戳、重要性等)。目前,我正在复制所有这些代码,所以我试图创建一个“BaseIndex”,并简单地让所有真正的索引类都继承自此。在
我试过了:
class BaseIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
object_id = indexes.IntegerField()
timestamp = indexes.DateTimeField()
class Meta:
abstract = True
class PersonIndex(BaseIndex):
...other fields...
但这给了我一个错误:
^{pr2}$所以我试着:
class BaseIndex(object):
text = indexes.CharField(document=True, use_template=True)
object_id = indexes.IntegerField()
timestamp = indexes.DateTimeField()
class PersonIndex(BaseIndex, indexes.SearchIndex, indexes.Indexable):
first_name = indexes.CharField()
middle_name = indexes.CharField()
last_name = indexes.CharField()
但这给了我一个错误:
SearchFieldError: The index 'PersonIndex' must have one (and only one) SearchField with document=True.
如何从自定义SearchIndex子类继承?在
只是不要将
indexes.Indexable
作为您不希望索引的任何内容的父级。在所以修改你的第一个例子。在
相关问题 更多 >
编程相关推荐