我有一個用戶模式,我有一個輪廓模型擴展它:Django的彈性搜索填入外鍵的字段
class ProfileIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
username = indexes.CharField(model_attr='user')
first_name = indexes.CharField(model_attr='first_name')
last_name = indexes.CharField(model_attr='last_name')
user = indexes.CharField(model_attr='user')
def get_model(self):
return Profile
def index_queryset(self, using=None):
"""Used when the entire index for the profile model is updated"""
return self.get_model().objects.all()
def prepare_tags(self, obj):
return [tag.name for tag in obj.tags.all()]
def prepare_username(self, obj):
return obj.user.username
def prepare_last_name(self, obj):
print obj.user.last_name, " : <== LAST NAME\n"
return obj.user.last_name#
def prepare_first_name(self, obj):
print obj.user.first_name, " : <== FIRST NAME\n"
return obj.user.first_name
我想要的搜索結果對象包含名字和用戶的姓氏是有在Django的默認用戶模型。我將如何獲得這樣的結果?
你在'model_attr'使用它呢? – AKS