djorm-ext-pgfulltext icon indicating copy to clipboard operation
djorm-ext-pgfulltext copied to clipboard

Unable to search when using GeoDjango

Open jensenbox opened this issue 12 years ago • 8 comments

I tried building a mixed manager but it still does not work. I thought I would throw it over to your fence and see what can be done.

This is the super simple manger I am using for 'objects':

class SearchGeoManager(SearchManagerMixIn, models.GeoManager):
    pass

When I do a search I get:

AttributeError: 'GeoQuerySet' object has no attribute 'search'

Perhaps some clues can be found in: https://docs.djangoproject.com/en/dev/topics/db/managers/

jensenbox avatar Dec 06 '12 20:12 jensenbox

Hello!

search method is only available on a manager. Your error report says that you are trying execute search method from queryset.

niwinz avatar Dec 06 '12 20:12 niwinz

I see that now - sorry about that. I guess the object was at the top of the filters so it escaped my view.

Is there a way to apply it like a filter?

Basically if there is a queryset, I want to pare down the result to things that only contain a string as well.

# ads = some_complicated_queryset

if search:
        ads = ads.search(search)

jensenbox avatar Dec 06 '12 20:12 jensenbox

Currently, you can not do this. But I will think about improving this part to be slightly more flexible.

How urgent is it for you?

niwinz avatar Dec 06 '12 21:12 niwinz

No, I have a workaround.

Sent from my iPhone

On 2012-12-06, at 1:33 PM, Andrey Antukh [email protected] wrote:

Currently, you can not do this. But I will think about improving this part to be slightly more flexible.

How urgent is it for you?

— Reply to this email directly or view it on GitHubhttps://github.com/niwibe/djorm-ext-pgfulltext/issues/5#issuecomment-11105535.

jensenbox avatar Dec 06 '12 21:12 jensenbox

Well! This weekend I'll have time and I will implement a more flexible method.

niwinz avatar Dec 06 '12 21:12 niwinz

Sorry, Definitely, I could not find time for it in a year :(

I keep open this issue because it should be implemented.

niwinz avatar Jan 04 '14 15:01 niwinz

I've met same issue. Using geodjango, django 1.6.5, postgres 9.3.4. I tried switching everything to from django.contrib.gis.db import models, but it didn't help. So i went investigating, a.k.a debugging, and this is how i made this work.

Just for the sake of separation (GeoDjango has it's own models and GeoManager from django.contrib.gis), I created:

in models.py:

class GeoSearchManager(SearchManagerMixIn, GeoManager):
    pass

in fields.py:

class GeoVectorField(geomodels.Field):
    def __init__(self, *args, **kwargs):
        kwargs['null'] = True
        kwargs['default'] = ''
        kwargs['editable'] = False
        kwargs['serialize'] = False
        kwargs['db_index'] = True
        super(GeoVectorField, self).__init__(*args, **kwargs)

    def db_type(self, *args, **kwargs):
        return 'tsvector'

    def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
        return self.get_prep_lookup(lookup_type, value)

    def get_prep_value(self, value):
        return value

and to make south happy, still in fields.py, at the end:

add_introspection_rules(rules=[], patterns=['djorm_pgfulltext\.fields\.GeoVectorField'])

After this, the search still didn't work, no errors were raised, search just keeps returning [] empty queryset, when i know there is at least one instance that should have been in there. After ipdbing a bit, I found out that if i explicitly pass fields into search, everything works ok. Following this, lead me to think that maybe update_search_field didn't fire, and so search_index is empty and that's why queryset is always empty, so i called update_search_feild() and did a search again, w.o the fields, this time everything worked as it should.

It all came down to explicitly running update_search_field() first time after adding search_index field to a model. Afterwards it all worked ok.

neara avatar Jun 30 '14 08:06 neara

The solution by @neara does not work for me, and still no fixes in master :(

Uznick avatar Feb 24 '16 21:02 Uznick