Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. class PlacesSerializer(serializers.ModelSerializer):
  2.     distance = serializers.SerializerMethodField()
  3.     address = serializers.SerializerMethodField()
  4.     location = serializers.SerializerMethodField()
  5.  
  6.     class Meta:
  7.         model = Place
  8.         fields = ['id', 'name', 'slug', 'logo', 'distance', 'address', 'location', 'description']
  9.  
  10.     def get_address(self, obj):
  11.         address = {}
  12.         address['address1'] = obj.address1
  13.         address['address2'] = obj.address2
  14.         address['city'] = obj.city.name
  15.         address['region'] = obj.region.name
  16.         address['country'] = obj.country.name
  17.         return address
  18.  
  19.     def get_location(self, obj):
  20.         location = {}
  21.         location['lat'], location['lon'] = obj.location.split(',')
  22.         return location
  23.  
  24.     def get_distance(self, obj):
  25.         point = self.context.get('point')
  26.         if point:
  27.             return obj.point.distance(point) * 100
  28.         else:
  29.             return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement