我使用ModelViewSet创建了一个Django rest api。部署在暂存和现在生产。生产工作正常,但登台有一些不正确的“下一个”网址。
在两个图像中看到“next”键,第一个图像是分段响应,第二个是生产响应。在分段下一个键中,“nub.staging.scoretrends.com”出现两次。两个都使用了代码。环境。在localhost上工作得很好。这样的事情发生了什么? ModelViewSet如下所示。
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)
这是我使用过的django rest配置
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',
)
}