我有下面显示的餐厅和评论模型。评论模型有餐厅的外国钥匙。如何在一些餐厅领域和评论模型的评论字段中进行搜索,该餐厅返回餐厅实例列表?

谢谢

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() 
有帮助吗?

解决方案

我认为您应该阅读手册: 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()]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top