1. <i id='gSPrP'><tr id='gSPrP'><dt id='gSPrP'><q id='gSPrP'><span id='gSPrP'><b id='gSPrP'><form id='gSPrP'><ins id='gSPrP'></ins><ul id='gSPrP'></ul><sub id='gSPrP'></sub></form><legend id='gSPrP'></legend><bdo id='gSPrP'><pre id='gSPrP'><center id='gSPrP'></center></pre></bdo></b><th id='gSPrP'></th></span></q></dt></tr></i><div id='gSPrP'><tfoot id='gSPrP'></tfoot><dl id='gSPrP'><fieldset id='gSPrP'></fieldset></dl></div>
      <legend id='gSPrP'><style id='gSPrP'><dir id='gSPrP'><q id='gSPrP'></q></dir></style></legend>
      • <bdo id='gSPrP'></bdo><ul id='gSPrP'></ul>

      <small id='gSPrP'></small><noframes id='gSPrP'>

        <tfoot id='gSPrP'></tfoot>

        根据查询参数进行筛选

        时间:2024-04-20

          <small id='Q5nzU'></small><noframes id='Q5nzU'>

              <bdo id='Q5nzU'></bdo><ul id='Q5nzU'></ul>

                <tbody id='Q5nzU'></tbody>
              <i id='Q5nzU'><tr id='Q5nzU'><dt id='Q5nzU'><q id='Q5nzU'><span id='Q5nzU'><b id='Q5nzU'><form id='Q5nzU'><ins id='Q5nzU'></ins><ul id='Q5nzU'></ul><sub id='Q5nzU'></sub></form><legend id='Q5nzU'></legend><bdo id='Q5nzU'><pre id='Q5nzU'><center id='Q5nzU'></center></pre></bdo></b><th id='Q5nzU'></th></span></q></dt></tr></i><div id='Q5nzU'><tfoot id='Q5nzU'></tfoot><dl id='Q5nzU'><fieldset id='Q5nzU'></fieldset></dl></div>
                <tfoot id='Q5nzU'></tfoot>

                • <legend id='Q5nzU'><style id='Q5nzU'><dir id='Q5nzU'><q id='Q5nzU'></q></dir></style></legend>
                • 本文介绍了根据查询参数进行筛选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用睡觉框架创建了我的接口,现在正在尝试对其进行筛选。

                  我的models.py是这样的:

                  class Airline(models.Model):
                      name = models.TextField()
                  
                  class Workspace(models.Model):
                      airline = models.ForeignKey(Airline)
                      name = models.CharField(max_length=100)
                  
                  class Passenger(models.Model):
                      workspace = models.ForeignKey(Workspace)
                      title = models.CharField(max_length=200)
                  

                  我希望在我的JSON文件中查看";特定工作区&或";特定航空公司&等的所有乘客。

                  这是我的serializers.py

                  class AirlineSerializer(serializers.ModelSerializer):
                      class Meta:
                          model = Airline
                  
                  
                  class WorkspaceSerializer(serializers.ModelSerializer):
                      class Meta:
                          model = Workspace
                  
                  
                  class PassengerSerializer(serializers.ModelSerializer):
                      class Meta:
                          model = Passenger
                  

                  views.py

                  class AirlineList(generics.ListCreateAPIView):
                      model = Airline
                      serializer_class = AirlineSerializer
                  
                  
                  class AirlineDetail(generics.RetrieveUpdateDestroyAPIView):
                      model = Airline
                      serializer_class = AirlineSerializer
                  
                  
                  class WorkspaceList(generics.ListCreateAPIView):
                      model = Workspace
                      serializer_class = WorkspaceSerializer
                  
                  
                  class WorkspaceDetail(generics.RetrieveUpdateDestroyAPIView):
                      model = Workspace
                      serializer_class = WorkspaceSerializer
                  
                  
                  class PassengerList(generics.ListCreateAPIView):
                      model = Passenger
                      serializer_class = PassengerSerializer
                  
                  
                  class PassengerDetail(generics.RetrieveUpdateDestroyAPIView):
                      model = Passenger
                      serializer_class = PassengerSerializer
                  

                  我想使用Filtering against query parameter,但我真的拿不到.

                  推荐答案

                  代码如下:

                  class PassengerList(generics.ListCreateAPIView):
                      model = Passenger
                      serializer_class = PassengerSerializer
                  
                      # Show all of the PASSENGERS in particular WORKSPACE
                      # or all of the PASSENGERS in particular AIRLINE
                      def get_queryset(self):
                          queryset = Passenger.objects.all()
                          workspace = self.request.query_params.get('workspace')
                          airline = self.request.query_params.get('airline')
                  
                          if workspace:
                              queryset = queryset.filter(workspace_id=workspace)
                          elif airline:
                              queryset = queryset.filter(workspace__airline_id=airline)
                  
                          return queryset
                  

                  这篇关于根据查询参数进行筛选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将字符串写入远程计算机上的文件? 下一篇:在DRF中使用HyperlinkedModelSerializer有什么好处?

                  相关文章

                      <bdo id='qqDx9'></bdo><ul id='qqDx9'></ul>
                  1. <i id='qqDx9'><tr id='qqDx9'><dt id='qqDx9'><q id='qqDx9'><span id='qqDx9'><b id='qqDx9'><form id='qqDx9'><ins id='qqDx9'></ins><ul id='qqDx9'></ul><sub id='qqDx9'></sub></form><legend id='qqDx9'></legend><bdo id='qqDx9'><pre id='qqDx9'><center id='qqDx9'></center></pre></bdo></b><th id='qqDx9'></th></span></q></dt></tr></i><div id='qqDx9'><tfoot id='qqDx9'></tfoot><dl id='qqDx9'><fieldset id='qqDx9'></fieldset></dl></div>

                    <legend id='qqDx9'><style id='qqDx9'><dir id='qqDx9'><q id='qqDx9'></q></dir></style></legend>
                  2. <small id='qqDx9'></small><noframes id='qqDx9'>

                  3. <tfoot id='qqDx9'></tfoot>