django haystack inheritance problems
I am using django and haystack for a search (obviously) and I dont want it including inherited objects. For example:
lets say I have model Person and model Employee(which inherits from Person). When an Employee object is added, it also creates a Person object. Person's dont have to be Employees though.
So I want to search all Person and Employee records, but exclude Person objects that开发者_StackOverflow中文版 are also Employees
I hope this makes sense,
Cheers
You can add a is_employee
field to your SearchIndex class for Person model.
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')
After that you can use this field to exclude the persons that are also Employees.
query = SearchQuerySet().filter(is_employee=False)
You can also replace this field with a more generic field person_type
if you have more than one person types.
精彩评论