我使用 ModelViewSet 创建了一个 Django rest api。在暂存中部署,现在在生产中。生产工作正常,但暂存有一些不正确的“下一个”网址。
查看两个图像中的键“next”,第一个图像是暂存响应,第二个是生产响应。在暂存下一个键中,“nub.staging.scoretrends.com”出现两次。两个环境中使用相同的代码。工作在本地主机中也很好。是什么让它发生了这样的事情?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',
)
}