7

我有如下所示的餐厅和评论模型。Comment 模型有一个指向 Restaurant 的 ForeignKey。如何在某些 Restaurant 字段和 Comment 模型的 comment 字段中执行搜索,该字段返回 Restaurant 实例列表?

谢谢

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() 
4

1 回答 1

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()]
于 2010-01-05T15:26:10.070 回答