Python django haystack继承问题
我正在使用django和haystack进行搜索(显然),我不希望它包括继承的对象。例如: 假设我有模范人物和模范员工(从Person继承)。添加Employee对象时,它还会创建Person对象。不过,员工不一定非得是员工 因此,我希望搜索所有Person和Employee记录,但排除同时也是Employee的Person对象 我希望这是有道理的Python django haystack继承问题,python,django,django-haystack,Python,Django,Django Haystack,我正在使用django和haystack进行搜索(显然),我不希望它包括继承的对象。例如: 假设我有模范人物和模范员工(从Person继承)。添加Employee对象时,它还会创建Person对象。不过,员工不一定非得是员工 因此,我希望搜索所有Person和Employee记录,但排除同时也是Employee的Person对象 我希望这是有道理的 干杯您可以将is_employee字段添加到Person模型的SearchIndex类中 class Person(models.Model):
干杯您可以将
is_employee
字段添加到Person模型的SearchIndex类中
class Person(models.Model):
# your existing code goes here
@property
def is_employee(self):
try:
self.employee # try to get the associated Employee object
return True
except Employee.DoesNotExist:
return False
class PersonSearchIndex(SearchIndex):
# your existing code goes here
is_employee = BooleanField(model_attr='is_employee')
之后,您可以使用此字段排除同时也是员工的人员
query = SearchQuerySet().filter(is_employee=False)
如果您有多个人员类型,还可以使用更通用的字段人员类型
替换此字段