Unable to paginate users
ShorthairBerry opened this issue · 4 comments
While using the get_users_newest_first() method it returns an Attribute error stating 'str' object has no attribute 'values'
views.py file
from rest_framework.response import Response
from rest_framework.decorators import api_view
from supertokens_python.syncio import get_users_newest_first
from .serializers import GetUserSerializer
@api_view(['GET'])
def get_users(request):
firstguys = get_users_newest_first()
serializer = GetUserSerializer(firstguys)
return Response(serializer.data)
serializers.py file
from attr import fields
from rest_framework import serializers
class GetUserSerializer(serializers.Serializer):
fields = '__all__'
Hey @ShorthairBerry, thanks for creating this issue.
afaik, fields = '__all__'
works only with Django Models (that too only with serializers inherited from serializers.ModelSerializer
)
So you should try this instead:
@api_view(['GET'])
def get_users(request):
firstguys = get_users_newest_first()
serializer = GetUserSerializer(firstguys.users, many=True) # note this line
return Response(serializer.data)
class GetUserSerializer(serializers.Serializer):
user_id = serializers.CharField()
email = serializers.EmailField()
recipe_id = serializers.CharField()
time_joined = serializers.IntegerField()
Also, this is a custom API View, so there will be more steps involved for integrating the default pagination into this. I'll share snippets for that in a while.
Here's what you can try to make it work with your default drf paginator:
views.py:
from rest_framework.decorators import api_view
from supertokens_python.syncio import get_users_newest_first
from rest_framework.settings import api_settings
@api_view(["GET"])
def get_users(request):
paginator = api_settings.DEFAULT_PAGINATION_CLASS()
firstguys = get_users_newest_first()
page = paginator.paginate_queryset(firstguys.users, request)
serializer = GetUserSerializer(page, many=True)
return paginator.get_paginated_response(serializer.data)
settings.py:
REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE": 10,
}
Let us know if you find any bugs or need help with something else :)
Closing this issue since it seems to be resolved.