Search code examples
tastypie

Tastypie get latest record


Instead of returning multiple objects when the ID isn't set. I'd like it to return only one, the latest.

# return latest entry
/api/entry/
{'id': 2, 'foo': 'bar', ...}

# return specified entry
/api/entry/1/
{'id': 1, 'foo': 'bar', ...}

I've tried overriding both obj_get_list and get_object_list, but neither worked for me.

Do I need to create a view for this?


Solution

  • Using aniav's solution, and adding the necessary modifications:

    Resource
        class Meta:
             max_limit=1
        get_object_list(self,request):
            return super(self, Resource).get_object_list(request).filter(user=request.user).order_by('-date')
    

    where of course date could be replaced with the field for which you want the latest of.