views:

240

answers:

1

hi,

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?

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  A: 

I think you should read the manual: http://haystacksearch.org/docs/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()]
diegueus9
you can search about MultiValue field and use it with comment_set attribute in restaurat model.
diegueus9
Thanks. I actually read the manual, but didn't know how to add the comments in my RestaurantIndex. Do you mean using:comment = indexes.CharField(model_attr='comment_set') in my RestaurantIndex? If so, how can I specify which fields of the Comment model must be indexed?
jul
No, i can't find the correct link but i mean something like: class RestaurantIndex(indexes.SearchIndex): comments = indexes.indexes.MultiValueField() def prepare_comments(self, obj): return [a for a in obj.comment_set.all()]
diegueus9