2010-01-05 12 views
7

Ho i modelli Restaurant e Comment mostrati di seguito. Il modello Comment ha un ForeignKey to Restaurant. Come posso eseguire una ricerca in alcuni campi del ristorante e nel campo dei commenti del modello Comment che restituisce un elenco di istanze di Restaurant?Ricerca in diversi tavoli con django-haystack

Grazie

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

risposta

3

Penso che si dovrebbe leggere il manuale: http://django-haystack.readthedocs.org/en/latest/tutorial.html

look per multivalore:

class RestaurantIndex(indexes.SearchIndex): 
    comments = indexes.MultiValueField() 
    def prepare_comments(self, obj): 
     return [a for a in obj.comment_set.all()] 
+0

è possibile cercare su campo MultiValue e utilizzarlo con comment_set attributo nel modello restaurat . – diegueus9

+0

Grazie. In realtà ho letto il manuale, ma non sapevo come aggiungere i commenti nel mio RestaurantIndex. Intendi usare: comment = indexes.CharField (model_attr = 'comment_set') nel mio RestaurantIndex? In tal caso, come posso specificare quali campi del modello Comment devono essere indicizzati? – jul

+0

No, non riesco a trovare il link corretto ma voglio dire una cosa del genere: classe RestaurantIndex (indexes.SearchIndex): commenti = indexes.indexes.MultiValueField() prepare_comments def (self, obj): ritorno [a per a in obj.comment_set.all()] – diegueus9

Problemi correlati