Nirbhay Kundan
Nirbhay Kundan

Reputation: 389

Mongoengine Serializer Error | Python | Django rest framework

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.

Upvotes: 1

Views: 1155

Answers (1)

snahor
snahor

Reputation: 1201

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.

Upvotes: 3

Related Questions