1

I am having trouble with serialization of "PointField" in mongoengine with django rest framework.

Following is my Model

 from mongoengine import *
 import datetime
  class Location(Document):
    user_id = StringField(required=True)
    location_title = StringField(required=False)
    location_type = StringField(required=False)
    coordinates = PointField(required=True)
    location_rating = IntField(required=True)
    reason = StringField(required=False)
    data_entry_date = DateTimeField(default=datetime.datetime.now)

Following is my serializer code

from rest_framework_mongoengine import serializers
from pycoreapi.models.location import Location
class LocationSerializer(serializers.MongoEngineModelSerializer):

    class Meta:
        model = Location
        depth = 3

and I am returning View Response from mongodb like this

filtered_objects = Location.objects(coordinates__geo_within_sphere=[[longitude, latitude], radius / 6371])

serializer = LocationSerializer(filtered_objects)

return serializer.data, status.HTTP_200_OK

Now from mongodb the filtered location list is coming fine but the Serializer is not working.

I am getting following error

Exception Value: 'PointField' object has no attribute '_get_val_from_obj'

Exception Location: ~/python2.7/site-packages/rest_framework/fields.py in field_to_native, line 422

Not getting any clue, please help.

1 Answer 1

3

The mongoengine extension for the rest-framework doesn't have a serializer for geo fields.

I think your best option is to create a custom field and serializer. For the serializer you can inherit from MongoengineModelSerializer and overwrite get_field.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks snahor. As suggested by you I have converted point field to list field and now its working as expected. Marking this as answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.