From e0608ed36366ca72586b3c3c9ff21081f076ffc2 Mon Sep 17 00:00:00 2001 From: Nikola Kolevski Date: Wed, 5 Aug 2015 10:38:40 +0000 Subject: [PATCH] Fix double underscore attribute fetching. --- rest_framework/pagination.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index b6be6b7cc..f5cc9640e 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -713,7 +713,22 @@ class CursorPagination(BasePagination): return tuple(ordering) def _get_position_from_instance(self, instance, ordering): - attr = getattr(instance, ordering[0].lstrip('-')) + attr_path = ordering[0].lstrip('-') + object_names = attr_path.split("__") + attr_name = object_names[-1] + + # cut off the attribute name + object_names = object_names[:-1] + + # start with the instance itself + obj = instance + + # fetch any other object + for object_name in object_names: + obj = getattr(obj, object_name) + + # and get the last split as the attribute + attr = getattr(obj, attr_name) return six.text_type(attr) def get_paginated_response(self, data):