views.py 823 B

123456789101112131415161718192021222324
  1. # Create your views here.
  2. import json
  3. from rest_framework import status
  4. from rest_framework.response import Response
  5. from rest_framework.viewsets import ModelViewSet
  6. from django.forms.models import model_to_dict
  7. from routes.models import Route, Route2User
  8. from routes.serializers import RoutesSerializer
  9. class RoutesView(ModelViewSet):
  10. queryset = Route.objects.all()
  11. serializer_class = RoutesSerializer
  12. pagination_class = None
  13. # 根据用户id 从关联表里 找它的路由id
  14. def list(self, request, *args, **kwargs):
  15. now_user_id = request.user.id
  16. route_ids = Route2User.objects.filter(user_id=now_user_id).values('route_id')
  17. routes = Route.objects.filter(id__in=route_ids).values()
  18. return Response(data=routes, status=status.HTTP_200_OK)