我有餐厅和评论模型如下所示。评论模型有一个ForeignKey到餐厅。如何在“注释”模型的某些“餐厅”字段和注释字段中执行搜索,以返回餐馆实例列表?
由于
class Restaurant(models.Model):
name = models.CharField(max_length=100)
country=models.ForeignKey(Country)
city=models.ForeignKey(City)
street=models.CharField(max_length=100)
street_number=models.PositiveSmallIntegerField()
postal_code=models.PositiveIntegerField(blank=True, null=True)
slug = models.SlugField(unique=True)
class Comment(models.Model):
user = models.ForeignKey(User)
restaurant = models.ForeignKey(Restaurant)
submit_date = models.DateTimeField(blank = True, null = False)
comment = models.TextField()
答案 0 :(得分:3)
我认为你应该阅读手册:http://django-haystack.readthedocs.org/en/latest/tutorial.html
寻找多元化:
class RestaurantIndex(indexes.SearchIndex):
comments = indexes.MultiValueField()
def prepare_comments(self, obj):
return [a for a in obj.comment_set.all()]