Skip to content Skip to sidebar Skip to footer

How To Serialize Django Geopt For Elasticsearch

How to define GeoPointField() in elasticsearch django. It shows a serialization error when i am trying to save the instance. i am using library 'django_elasticsearch_dsl code: from

Solution 1:

The problem is that django_elasticsearch_dsl (and further, elasticsearch_dsl) doesn't know how to serialize that custom django_google_maps.fields.GeoPt object into a format understood by Elasticsearch.

Quoting the docs, the object will need to have a to_dict() method.

The serializer we use will also allow you to serialize your own objects - just define a to_dict() method on your objects and it will automatically be called when serializing to json.

You should be able to monkey-patch that method in with something like (dry-coded)

from django_google_maps.fields import GeoPt

GeoPt.to_dict = lambda self: {'lat': self.lat, 'lon': self.lon}

early in your app's code (an AppConfig ready() method is a good choice, or failing that, a models.py, for instance)

Solution 2:

The issue is that get_lat_long() returns an object of type django_google_maps.fields.GeoPt and you cannot assign is to your geolocation object. But modifying the code like below might do the trick:

 user = GutitUser.objects.get(phone_number=phone_number)
 geo_point = get_lat_long()
 user.geolocation.lat = geo_point.lat
 user.geolocation.lon = geo_point.lon
 user.save()

Post a Comment for "How To Serialize Django Geopt For Elasticsearch"