Check in the database only the fields specified in the serializer django-rest-framework

0

I have the following serilizers

  class MarcaSerializer(serializers.ModelSerializer):
      class Meta:
          model = Marca
          fields = ("codigo",)


  class AutoSerializer(WritableNestedModelSerializer):
     marca = MarcaSerializer()

     class Meta:
         model = Auto
         fields = ("nombre", 'marca')

When I use the api, through django-debug-tolbar, I can verify that it performs a consulata to the BD, where it calls all the fields of the models, and not necessarily to those that it specifies in the fields of each serializer , as I do so that it only extracts from the BD the fields specified in the fields ViewSetModel

  class MarcaViewSet(viewsets.ModelViewSet):
      queryset = Auto.objects.all().select_related("marca")
      serializer_class = AutoSerializer

      def list(self, request, *args, **kwargs):
          queryset = self.queryset
          serializer = self.get_serializer(queryset, many=True)
          return Response(serializer.data)
    
asked by Jefferson Tenecota 20.06.2018 в 00:37
source

0 answers