I've got the Restaurant and Comment models shown below. The Comment model has a ForeignKey to Restaurant. How can I perform a search in some of the Restaurant fields and in the comment field of the Comment model which returns a list of Restaurant instances?
我有餐厅和评论模型如下所示。评论模型有一个ForeignKey到餐厅。如何在“注释”模型的某些“餐厅”字段和注释字段中执行搜索,以返回“餐馆”实例列表?
Thanks
谢谢
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()
1 个解决方案
#1
3
I think you should read the manual: http://django-haystack.readthedocs.org/en/latest/tutorial.html
我想你应该阅读手册:http://django-haystack.readthedocs.org/en/latest/tutorial.html
look for multivalue:
寻找多元化:
class RestaurantIndex(indexes.SearchIndex):
comments = indexes.MultiValueField()
def prepare_comments(self, obj):
return [a for a in obj.comment_set.all()]
#1
3
I think you should read the manual: http://django-haystack.readthedocs.org/en/latest/tutorial.html
我想你应该阅读手册:http://django-haystack.readthedocs.org/en/latest/tutorial.html
look for multivalue:
寻找多元化:
class RestaurantIndex(indexes.SearchIndex):
comments = indexes.MultiValueField()
def prepare_comments(self, obj):
return [a for a in obj.comment_set.all()]