私は、緯度と経度に基づいて近接検索結果にGeoDjango GISを使用しています。REST応答で距離フィールドを取得するには?
class Address(gismodels.Model):
# partner = models.ForeignKey(Partner, related_name="partner_addresses", on_delete=models.CASCADE)
street = gismodels.CharField(max_length=255)
city = gismodels.CharField(max_length=255)
postcode = gismodels.CharField(max_length=255, null=True, blank=True, default='0000')
country = gismodels.CharField(max_length=255)
businesses = gismodels.OneToOneField(Business, related_name="business_address", on_delete=models.CASCADE, null=True)
latitude = gismodels.FloatField(null=True, blank=True)
longitude = gismodels.FloatField(null=True, blank=True)
location = gismodels.PointField(null=True, blank=True)
objects = gismodels.GeoManager()
def __unicode__(self):
return self.street
class Meta:
verbose_name = "Address"
verbose_name_plural = "Addresses"
def save(self, *args, **kwargs):
if self.latitude and self.longitude:
self.location = Point(self.longitude, self.latitude)
super(Address, self).save(*args, **kwargs)
そして、そうシリアライザは次のとおりです:
モデルは、静かでシンプルですので
class AddressSerializer(HyperlinkedModelSerializer):
name_of_business = serializers.CharField(source="businesses.name_of_business")
business_id = serializers.IntegerField(source="businesses.pk")
opening_time = serializers.TimeField(source="businesses.business_details.opening_time")
class Meta:
model = Address
fields = ('pk', 'distance', 'street', 'city', 'postcode',
'name_of_business', "business_id",
'opening_time')
、私は応答を取得しようとすると、curl http://127.0.0.1:5000/api/businesses/37.4418834/-122.14301949999998/
を使用して言う、私はこのエラーは、関連する取得します距離フィールドへ:
Traceback (most recent call last):
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
return self.application(environ, start_response)
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/whitenoise/base.py", line 66, in __call__
return self.application(environ, start_response)
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
response = self.get_response(request)
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/django/core/handlers/base.py", line 218, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/django/core/handlers/base.py", line 261, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/rest_framework/views.py", line 477, in dispatch
response = self.handle_exception(exc)
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/rest_framework/views.py", line 437, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/dff/.virtualenvs/dff/lib/python2.7/site-packages/rest_framework/views.py", line 448, in raise_uncaught_exception
raise exc
ImproperlyConfigured: Field name `distance` is not valid for model `Address`.
ここは私のapiビューです:
class ProximityFilter(ListAPIView):
serializer_class = AddressSerializer
def get_queryset(self):
longitude = self.kwargs.get('longitude')
latitude = self.kwargs.get('latitude')
current_point = GEOSGeometry('POINT(%s %s)' % (longitude, latitude), srid=4326)
queryset = Address.objects.filter(city="SomeCity",location__distance_lte=(current_point, D(mi=700000000))).distance(
current_point).order_by('distance')
return queryset
これを解決してREST応答で距離を得る方法はありますか? 手がかりはありますか?誰でも? :)
おかげ
私はDjangoの1.8を使用しています。
:あなたは、彼らが最初に
Distance()
機能を使用して注釈を付ける必要がありますか?組み込みのDistanceToPointFilterを使用できるためです。 – ozgur@ozgur私はそれを見ましたが、もし私がそれを使うことができるかどうか、あるいは別のlibを使わずにこれを簡単に解決できるかどうかは分かりませんでした。とにかく、ありがとうございました。 – Maverick