Unable to search on parent fields.
jaysonsantos opened this issue · 6 comments
ie.:
class Product(models.Model):
name = models.CharField(u'Name', max_length=100, null=False)
slug = models.SlugField(u'Slug', max_length=100, null=False, blank=True)
description = models.TextField(u'Description', null=True)
search_index = VectorField()
objects = SearchManager(
fields=('name', 'description')
)
class StoreProduct(models.Model):
price = models.DecimalField(u'Price', max_digits=10, decimal_places=2)
office = models.ForeignKey('stores.Office')
product = models.ForeignKey(Product)
in_stock = models.BooleanField(u'in stock', default=True)
One office can have a lot of products, and in this case i have to find products on store and ideal world would be:
StoreProduct.objects.search('test', fields=['product__name', 'product__description']).filter(in_stock=True)
Is there any way to do that?
Hi!
Currently, is not possible search on foreignKeys relations, but is possible to implement. I put this feature to my todo list for this package.
Thanks.
My dirty local fix is:
query = StoreProduct.objects.select_related('product').extra(
where=['products_product.search_index @@ plainto_tsquery(%s)'],
params=[form_data['q']])
So good, this solves the problem. But it would be nice to integrate a solution in the package.
Furthermore, the fields kwarg on search method indicates to a manager not use a vector field. Be careful with this.
Do you plan on implementing this feature? It would be very cool.
This was difficult to do in django <1.7, but it will be implemented support for django 1.7 using custom lookups.
@linuxlewis do you think this feature will be added sometime soon? This would be super useful for a bunch of projects I'm collaborating on :)