[Django]-Django TastyPie Geo Distance Lookups

4👍

The fields defined in the meta attributes aren’t enough to have the additional values returned.
They need to be defined as additional fields in the resource:

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

This value can be filled by defining dehydrate_distance method inside the resource

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

or by adding some additional elements to queryset in resources meta like so:

queryset = YourModel.objects.extra(select={'distance': 'SELECT foo FROM bar'})

Tastypie itself appends a field called resource_uri that isn’t actually present in the queryset, looking at the source code of tastypie’s resources might be helpful for you too.

Leave a comment