linuxlewis / djorm-ext-pgfulltext

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

Simple fix for thrown exception when SearchManager search_field is None #72

Open hfickes opened 8 years ago

hfickes commented 8 years ago

If your model has: objects = SearchManager(fields=None, search_field=None)

so you can do searches with

table.objects.search('text to search', fields=('field1', 'field2'))

then the djorm_pgfulltext/models.py will throw an exception from within SearchQuerySet's search() method at line 285:

            full_search_field = "%s.%s" % (
                qn(self.model._meta.db_table),
                qn(self.manager.search_field)
            )

because self.manager.search_field is None so qn throws the exception.

The fix for this is simple, move that block of code down a few lines to be where it is actually used:

        # if fields is passed, obtain a vector expression with
        # these fields. In other case, intent use of search_field if
        # exists.
        if fields:
            search_vector = self.manager._get_search_vector(config, using, fields=fields)
        else:
            **full_search_field **= "%s.%s" % (
                qn(self.model._meta.db_table),
                qn(self.manager.search_field)
            )
            if not self.manager.search_field:
                raise ValueError("search_field is not specified")

            search_vector = **full_search_field**

We will probably fork the code locally for this fix, but will undo our fork once its fixed in this or some other fashion.

Thank you for writing this package, we really appreciate it, -herb