尝试django rest框架,有可能有2个端点根据同一模型返回不同的数据吗? 我需要这样的东西。
api / tarjetas应该返回创建的所有对象,而api / tarjetasusuario应该返回由特定用户创建的对象。
由于
class TarjetaViewSet(viewsets.ModelViewSet):
queryset = Tarjeta.objects.all()
serializer_class = TarjetaSerializer
permission_classes = (OwnerPermission,)
class TarjetasusuarioViewSet(APIView):
queryset = Tarjeta.objects.all()
serializer_class = TarjetaSerializer
permission_classes = (OwnerPermission,)
def get_queryset(self):
return Tarjeta.objects.filter(usuario=self.request.user)

答案 0 :(得分:0)
尝试使用此参考
class TarjetaViewSet(viewsets.ModelViewSet):
queryset = Tarjeta.objects.all()
serializer_class = TarjetaSerializer
permission_classes = (OwnerPermission,)
class TarjetasusuarioViewSet(APIView):
user = User.objects.get(username=request.user)
queryset = Tarjeta.objects.filter(usuario=user)
serializer_class = TarjetaSerializer
permission_classes = (OwnerPermission,)
答案 1 :(得分:0)
我认为你必须从TarjetasusuarioViewSet类中的ModelViewSet扩展:
class TarjetaViewSet(viewsets.ModelViewSet):
queryset = Tarjeta.objects.all()
serializer_class = TarjetaSerializer
permission_classes = (OwnerPermission,)
class TarjetasusuarioViewSet(viewsets.ModelViewSet):
queryset = Tarjeta.objects.all()
serializer_class = TarjetaSerializer
permission_classes = (OwnerPermission,)
def get_queryset(self):
return Tarjeta.objects.filter(usuario=self.request.user)