photocrowd / django-cursor-pagination

Cursor-based pagination for Django
BSD 3-Clause "New" or "Revised" License
156 stars 27 forks source link

Consider applying existing ordering of queryset, rather than requiring ordering as arg. #8

Open adamkowalczyk opened 8 years ago

adamkowalczyk commented 8 years ago

CursorPaginator takes 2 parameters queryset and ordering and applies the ordering to the queryset on init.

However, if a query set is already adequately ordered for pagination purposes, either explicitly or on the model's Meta class, it would be convenient to be able to infer and user that ordering and to call CursorPaginator with a single queryset argument.

For example:

class CursorPaginator(object):

    def __init__(self, queryset, ordering=None):
        self.queryset = queryset
        self.ordering = ordering

        if ordering:
            self.queryset = queryset.order_by(*ordering)
        elif queryset.query.order_by:
            self.ordering = queryset.query.order_by
        elif queryset.query.get_meta().ordering
            self.ordering =  queryset.query.get_meta().ordering
        else:
            raise InvalidCursor('No ordering supplied of inferable')