我正在尝试使用ModelViewSet。我现在正面临这个错误
这是我的观点集=>
class ShiftViewSet(viewsets.ModelViewSet):
queryset = Shift.objects.all()
serializer_class = ShiftSerializer()
# filter_backends = (filters.DjangoFilterBackend,)
# filterset_fields = ('shiftid',)
@action(methods=['get'], detail=False)
def newest(self, request):
newest = self.get_queryset().order_by('Created_DT').last()
serializer = self.get_serializer_class()(newest)
return Response(serializer.data)
@action(methods=['get'], detail=False)
def shiftsum(self, request):
query = (
Shift.objects.values('shiftid')
.annotate(shiftdesc=Max('shiftdesc'))
.annotate(overnight=Max('overnight'))
.annotate(isspecialshift=Max('isspecialshift'))
.annotate(ct=Count('*')) # get count of rows in group
.order_by('shiftid')
.distinct()
)
serializer = ShiftSummarySerializer(query,many=True)
return Response(serializer.data)
@action(methods=['get'], detail=False)
def byshiftid(self, request):
shiftid = self.request.query_params.get('shiftid',None)
query = self.get_queryset().filter(shiftid=shiftid)
serializer = ShiftSerializer(query,many=True)
return Response(serializer.data)
这是我的路由器和url =>
router.register('shifts_mas', ShiftViewSet, base_name='shifts')
path('api/', include(router.urls))
通常我可以像/ api / shifts_mas /一样打电话,我会获得shift
的所有记录,但是现在我收到了这个错误,我也不知道为什么。我可以知道为什么吗?
答案 0 :(得分:2)
您应该拥有一个序列化器类,而不应将其作为serializer_class
属性的类实例
serializer_class = ShiftSerializer # No parenthesis here