在Django haystack搜索结果中显示外键值
我刚接触django和haystack,能不能帮我看看这段代码?
class UserProfile(models.Model):
street_address = models.CharField(max_length=100, blank=False, null=False)
city = models.CharField(max_length=100, blank=False, null=False)
zip_code = models.PositiveIntegerField(blank=False, null=True)
class UserProfessional(models.Model):
userprofile = models.ForeignKey(UserProfile, unique=True, related_name = 'user_professionals')
company = models.CharField(max_length=200, blank=False, null=False)
professional_type = models.CharField(max_length=50, choices=PROFESSIONAL_TYPE)
bio_exp = models.CharField(max_length=300)
我们要根据邮政编码、城市和州来搜索。所以我们可以通过邮政编码来查找,但结果中应该显示公司名称和简介。
1 个回答
1
我希望你已经创建了一个搜索索引,这个索引会关注UserProfessional,而不是UserProfile。
class UserProfessionalIndex(indexes.SearchIndex, indexes.Indexable):
userprofile = indexes.CharField(model_attr='userprofile')
def get_model(self):
return UserProfessional
然后在搜索模板中指定
{{ object.userprofile.zip_code }}
{{ object. bio_exp }}
现在搜索zip_code和其他参数,你将会从UserProfessional中得到bio_exp和其他字段的信息。
希望这对你有帮助。