Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching in several tables with django-haystack

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() 
like image 829
jul Avatar asked Jan 05 '10 11:01

jul


1 Answers

I think you should read the manual: 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()]
like image 61
diegueus9 Avatar answered Nov 03 '22 07:11

diegueus9