etianen / django-watson

Full-text multi-table search application for Django. Easy to install and use, with good performance.
BSD 3-Clause "New" or "Revised" License
1.21k stars 129 forks source link

Searching related fields #271

Closed pljspahn closed 4 years ago

pljspahn commented 4 years ago

I have the following models:

class Product(models.Model):
    name = models.CharField('Name', max_length=50)
    description = models.TextField('Description', max_length=1000)

    def __str__(self):
        return self.name

class PlantProduct(Product):
    genus = models.ForeignKey(Genus, on_delete=models.SET_NULL, null=True, blank=True)        

class Genus(models.Model):
    common = models.CharField('Common Genus', max_length=100)
    latin = models.CharField('Latin Genus', max_length=100)

    def __str__(self):
        return self.common

I have registered PlantProduct with watson without any additional options:

class CatalogConfig(AppConfig):
    name = 'catalog'

    def ready(self):
        PlantProduct = self.get_model('PlantProduct')
        watson.register(PlantProduct)

I am able to search and it's fine. I would like to add the genus foreign key to the search, specifically the latin name, so I changed the model registration to:

watson.register(PlantProduct, fields=("genus__latin"))

After making this change, I now run manage.py buildwatson which fails with the error: watson.search.SearchAdapterError: Could not find a property called 'g' on either <PlantProduct: Mission Arborvitae> or <watson.search.CustomSearchAdapter object at 0x7f7ea2ceb610>

I am using Django 3.0.5 with PostgreSQL - Am I trying to do something that's not supported?

pljspahn commented 4 years ago

=/

It never fails that after I post an issue or question, I find out the problem immediately after.

watson.register(PlantProduct, fields=("genus__latin"))

was missing the trailing comma

watson.register(PlantProduct, fields=("genus__latin",))

Fixed it.

etianen commented 4 years ago

Yup, that's the way it always goes!

Thanks for posting the solution. Means the next person with the problem might avoid the next pitfall, and keeps the open issue list small.