Django rest pagination next url is invalid

I created Django rest api using ModelViewSet. It is deployed at the stage of production and now in production.Production works fine, but the production has an incorrect "next" url.

enter image description here enter image description here

Look at the “next” key in both images, the first image is the production response, and the second is the production answer. In the next step, "nub.staging.scoretrends.com" appears. This code is used as in environment.Working fine in localhost too.What does this happen like this? An example of a ModelViewSet is shown below.

class EntityDetail(viewsets.ModelViewSet):
    """
    Retrieve a spider.
    """
    queryset = Entity.objects.all()
    serializer_class = EntitySerializer
    filter_backends = (filters.DjangoFilterBackend, filters.OrderingFilter)
    filter_class = EntityFilter
    filter_fields = ('name', 'entity_type', 'gender', 'active', 'verified', 'date_created', 'date_modified')
    ordering_fields = ('name', 'date_created', 'date_modified')

    def get_queryset(self):
        queryset = Entity.objects.all()
        name = self.request.query_params.get('name', None)
        industry = self.request.query_params.get('industry', None)
        entity_types = self.request.query_params.get('entity_types', None)
        if name:
          queryset = Entity.objects.filter(name__icontains=name)
        if industry:
          queryset = Entity.objects.filter(meta__primary_industry__iexact=industry)
        if entity_types:
          entity_types = [int(id) for id in entity_types.split(',')]
          queryset = queryset.filter(entity_type__in=entity_types)
        return queryset

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        if request.query_params.get('nopaginate') is None:
          page = self.paginate_queryset(queryset)
          if page is not None:
            serializer = EntitySerializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = EntitySerializer(queryset, many=True)
        return Response(serializer.data)

   def update(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = EntitySerializer(instance, data=request.data, partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save(entity_id=instance.pk, **serializer.validated_data)
        return Response(serializer.data)

   def partial_update(self, request, *args, **kwargs):
       kwargs['partial'] = True
       return self.update(request, *args, **kwargs)

This is the django break configuration I used

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 20,
    'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    )
}
+4
source share

Source: https://habr.com/ru/post/1683740/


All Articles