Django Database ID Breakdown

I created an API with the Django Rest Framework. I want to change the pagination to improve the user interface.

Problem :

The client addresses all requests. The request looks like this:

http://website.com/api/v1/posts/?page=1

This returns the first page of messages. However, new posts are always being created. Therefore, when the user requests:

http://website.com/api/v1/posts/?page=2

Posts almost always coincide with pages 1 (since new data always arrives and we order -created).

Possible Solution?

I had an idea to send the identifier of the object along with the request so that when capturing messages. We take them in relation to the last request.

http://website.com/api/v1/posts/?page=2&post_id=12345

And when we paginate, we filter where post_id < 12345

But this only works if our post_id is an integer.

ListAPIView

class PostList(generics.ListAPIView):
    """
    API endpoint that allows posts to be viewed
    """
    serializer_class = serializers.PostSerializer # just a basic serializer
    model = Post

? .

+4
3

? , . , , .

- :

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)

, , , URL :

http://website.com/api/v1/posts/?refersh=whatever

.

UPDATE

, - ( ): .

, .

,

TimeStamped Post .

, , , , :)

+1

CursorPagination DRF:

Pagination :

  • . CursorPagination , , .
  • . . .

-created , .

+1

Perhaps you can add a field for each object, for example, "created_at / updated_at". Then you can save the timestamp when the user has completed the request and filtered everything that came after it.

I haven’t tried it myself, but I think it might work on your case.

0
source

All Articles