Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django haystack继承问题_Python_Django_Django Haystack - Fatal编程技术网

Python django haystack继承问题

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):

我正在使用django和haystack进行搜索(显然),我不希望它包括继承的对象。例如:

假设我有模范人物和模范员工(从Person继承)。添加Employee对象时,它还会创建Person对象。不过,员工不一定非得是员工

因此,我希望搜索所有Person和Employee记录,但排除同时也是Employee的Person对象

我希望这是有道理的


干杯

您可以将
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)
如果您有多个人员类型,还可以使用更通用的字段
人员类型
替换此字段