2012-09-05 6 views
3

지형 거리 검색에 TastyPie를 사용하고 있습니다. TastyPie가 공식적으로 지원하지 않기 때문에 약간 어렵습니다. Github에서 (https://gist.github.com/1067176)에 다음 코드 샘플 발견Django TastyPie Geo 거리 검색

def apply_sorting(self, objects, options=None): 
    if options and "longitude" in options and "latitude" in options: 
     return objects.distance(Point(float(options['latitude']), float(options['longitude']))).order_by('distance') 

    return super(UserLocationResource, self).apply_sorting(objects, options) 

그것은 잘 작동을하지만, 지금은 TastyPie의 필드 결과로 거리를 가지고 싶습니다. 어떻게 할 생각인가? fields 속성에 "distance"를 포함하면 작동하지 않습니다.

미리 도움 주셔서 감사합니다.

답변

4

메타 속성에 정의 된 필드가 추가 값을 반환하기에 충분하지 않습니다.

이 값은 리소스

def dehydrate_distance(self, bundle): 
    # your code here 

내부 dehydrate_distance 방법을 정의하여 또는 추가적인 구성 요소를 추가하여 작성 될 수

distance = fields.CharField(attribute="distance", default=0, readonly=True) 

자원 메타에서의 검색어하기 : 그들은 자원의 추가적인 필드로 정의 될 필요 like :

Tastypie 자체는 실제로 que에없는 필드 인 resource_uri를 추가합니다 tastypie 리소스의 소스 코드를 보면 ryset도 도움이 될 것입니다.

+1

그게 효과가 있습니다! 너무 고마워, 정확히 내가 원했던거야! 이 값은 실제로 .distance() geodjango 함수로 채워지므로 해당 필드 정의를 추가해야합니다. 다시 한번 감사드립니다! –