Closed rollue closed 5 years ago
This is not the place to ask for support, especially not support on django restframework. Please try using LimitOffsetPagination
The docs of DRF clearly explain how you can specify the ordering field: https://www.django-rest-framework.org/api-guide/pagination/#details-and-limitations
This is the way how to do it.
@specialunderwear @maerteijn Thanks. I'll be more careful with questions next time :)
This is for anyone trying to use CursorPagination with oscar-api
created
by default.created
as model field. Most of them have either date_created
or not have any timestamp-related field at all(ex. Country Model, ProductClass Model etc.)created
- DRF CursorPagination's default settingdate_created
- most of the models in django-oscar uses this instead of created
id
- django's default is incremental field, meaning it can be used for orderingI include my working code hoping it might help someone. Good luck.
from django.core.exceptions import FieldError
from rest_framework.pagination import CursorPagination
class CustomCursorPagination(CursorPagination):
def paginate_queryset(self, queryset, request, view=None):
"""
In oscar/api/basket endpoint, the queryset type is not django's default queryset:
it is QuerySetList defined in oscarapi.views.utils.QuerysetList. QuerySetList has
queryset field, so queryset.queryset is passed as parameter.
"""
if not hasattr(queryset.model, "created"):
self.ordering = ["-date_created"]
try:
return super().paginate_queryset(queryset, request, view=None)
except AttributeError:
queryset = queryset.queryset # for oscarapi/basket/
except FieldError:
if hasattr(queryset.model, "id"):
self.ordering = ["-id"]
else:
self.ordering = queryset.model.Meta.ordering # for oscarapi/coutries/
return super().paginate_queryset(queryset, request, view=None)
I'm trying to use this library for our new service that need ecommerce-related backend APIs.
I have DRF's CursorPagination as our default pagination class. This orders queryset with
created
field, assuming thatthere must be a 'created' timestamp field on the model instances
.Django-oscar, however, seems to have
date_created
as the default timestamp field.This means either of our previously-developed apps, or the django-oscar related apps should change the timestamp field name. Then change the CursorPagination's ordering field to the unified timestamp field, which should either be
date_created
orcreated
.Am I getting this right? Or is there any other way?
Changing the fields names on our previous work, or forking django-oscar just for this seem too laborious.
Thanks.