I have built an API with Django Rest Framework. I want to change pagination for a better user experience.
The client makes a call to request all posts. The request looks like:
post_id < 12345
API endpoint that allows posts to be viewed
serializer_class = serializers.PostSerializer # just a basic serializer
model = Post
How about caching the queryset? So that the next page is served from the same query set, and not from a new one. And then you could use a parameter to get a new queryset when you want.
Something like this:
from django.core.cache import cache class PostList(generics.ListAPIView): def get_queryset(self): qs_key = str(self.request.user.id) + '_key' if 'refresh' in self.request.QUERY_PARAMS: # get a new query set qs = Post.objects.all() cache.set(qs_key, qs) return cache.get(qs_key)
So basically, only when your url will be like this:
the request will return new data.
In order to provide each user with it's own set of posts, the cache key must contain an unique identifier (which might be the user's ID): I also updated the code.
The downside to this approach is that for a very large number of users and a large number of posts for each user, it might not work very well.
So, here is my second idea
Use a TimeStamped model for the Post model, and filter the query set based on the created field.
I don't know much about your models and how exactly they are built, so I guess you will have to choose which solution is best for your app :)