linuxlewis / djorm-ext-pgfulltext

PostgreSQL full-text search integration with django orm.
Other
250 stars 84 forks source link

Unable to search on parent fields. #7

Open jaysonsantos opened 11 years ago

jaysonsantos commented 11 years ago

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?

niwinz commented 11 years ago

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.

jaysonsantos commented 11 years ago

My dirty local fix is:

query = StoreProduct.objects.select_related('product').extra(
            where=['products_product.search_index @@ plainto_tsquery(%s)'],
            params=[form_data['q']])
niwinz commented 11 years ago

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.

clime commented 11 years ago

Do you plan on implementing this feature? It would be very cool.

niwinz commented 10 years ago

This was difficult to do in django <1.7, but it will be implemented support for django 1.7 using custom lookups.

mkuchen commented 9 years ago

@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 :)