Question

I have the following problem with Django 1.6, Python 3.3 and haystack. Basically I'm trying to use LocationField in haystack, but unfortunately not very successfully.

Here is the exception, which python trow.

<Point object at 0x7fe54e41d9a0> is not JSON serializable

And my code in search_indexes.py:

class ListingIndex(indexes.SearchIndex, indexes.Indexable):
    ....
    location = indexes.LocationField(model_attr='get_location')
    ....

    def get_model(self):
        return Listing

And in the models.py():

...
from haystack.utils.geo import Point
...

class Listings(models.Model):
    ...
    def get_location(self):
        return Point(self.lng, self.lat)

I have searched for a several hours in Google and SO and learn a lot of for serialization Django objects, but doesn't find any result, which help me to found a solution here. Thanks in advance.

Was it helpful?

Solution

I found my the solution here. The issue was that when I update the Elasticsearch, pass as parameter for the location Point object(as the field was from type Location, I thought that is the right way to do that), but actually, that what I should to do is to pass a json with. Below is the code:

location = self.get_location()
location_es = "{0},{1}".format(location.y, location.x)

And after that, when I updating Elasticsearch, using location_es, not location as parameter for the location field. Thank you for your answers ! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top