'Django exclude repetition in another request

There are two requests, the first is reading the task by uuid, the second is outputting 3 random tasks from the same user - "recommendations"

The task that is open

  {
        "id": 4,
        "userInfo": 1,
        "title": "Comparing numbers",
        "uuid": "5a722487"
    }

Recommendations for it

Tell me, how to exclude the current task from the second query

 [
        {
            "id": 16,
            "userInfo": 1,
            "title": "The opposite number",
            "uuid": "1e6a7182"
        },
        {
            "id": 19,
            "userInfo": 1,
            "title": "Number of vowels",
            "uuid": "be1320cc"
        },
        {
            **"id": 4, <- exclude this post
            "userInfo": 1,
            "title": "Comparing numbers", 
            "uuid": "5a722487"**
        }
    ]

views.py

class PostUuid(generics.ListAPIView):
    """Reading a record by uuid"""

    queryset = Task.objects.all()
    serializer_class = TaskCreateSerializer
    lookup_field = 'uuid'

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        Task.objects.filter(pk=instance.id)
        serializer = self.get_serializer(instance)
        return Response(serializer.data)

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)


class RecommendationTaskView(generics.ListAPIView):
    """Getting a recommendation"""

    serializer_class = TaskCreateSerializer

    def get_queryset(self):


        items = list(Task.objects.filter(
            userInfo_id=self.kwargs.get('pk')).select_related('userInfo'))
        random_items = random.sample(items, 3)
        return random_items


Solution 1:[1]

Restful APIs should be stateless. Statelessness means that every HTTP request happens in complete isolation. When the client makes an HTTP request, it includes all information necessary for the server to fulfill the request.

The server never relies on information from previous requests from the client. If any such information is important then the client will send that as part of the current request.

You should send the task id which you want to exclude on the other apis. In this way, you have that id and you can exclude that on the query set.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammad sadegh borouny