0
class ABC(generics.ListCreateApiView):
         @swagger_auto_schema(
             operation_description="THIS API IS TO CREATE MESSAGES IN A LIST ",
             auto_schema=AcceptFormDataSchema,
             request_body=MessageGetSerializer
         )
         def get_queryset(self):
             data =self.request.GET.get("code")
             ...
         @swagger_auto_schema(
             operation_description="THIS API IS TO CREATE MESSAGES IN A LIST ",
             request_body=openapi.Schema(
                 type=openapi.TYPE_OBJECT,
                 required=["data"],
                 properties={
                    "code": openapi.Schema(type=openapi.TYPE_STRING),
                 },
             )
         )
         def post(self, request):
             brand_code = request.data.get("code")
             ...
#serializer.py

class MessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Messages
        fields = ("message_id", "content", "description")

My post method is working fine with the fields which I required using the same serializer but it's not working for the get_queryset method. Can anyone please suggest something on how I will get the fields using drf-yasg?

EDG956
  • 797
  • 8
  • 23
ast
  • 1
  • What do you want to accomplish? FYI `get_queryset` is an internal function of `ViewSet`s and it is not an endpoint, thus it shouldn't appear in swagger. You shouldn't try to use an internal function as a view handler, you'll break your `ViewSet`. – EDG956 Sep 26 '22 at 07:26
  • Again, what do you want to accomplish? an endpoint to list Messages? – EDG956 Sep 26 '22 at 10:17
  • I was accomplishing the docs for all API and by default, this swagger module drf-yasg is considering this get_queryset as a get API. – ast Sep 26 '22 at 10:26
  • The reason it may pick up a `get` query may probably be the inheritance from [`ListCreateApiView`](https://www.django-rest-framework.org/api-guide/generic-views/#listcreateapiview), which provides both `get` and `post` methods. This endpoint is implicitly created by the ApiView class and in most cases you don't need to overwrite it. If you wish to document that, you can follow the first tip from [`drf-yasg`](https://drf-yasg.readthedocs.io/en/stable/custom_spec.html#the-swagger-auto-schema-decorator) and use Django's `method_decorator`. – EDG956 Sep 26 '22 at 10:54

1 Answers1

0

You shouldn't decorate get_queryset as that is an internal function and not part of the ApiView's endpoints. You probably see a get request in swagger because the ListCreateApiView you're using defines handlers for get and post methods.

Since you're not overriding the get method handler, you can inject a decorator into the ApiView's get method using Django's method_decorator, as indicated in drf-yasg section on swagger_auto_schema decorator.

The following is an example implementation to your ApiView that should also document the get endpoint.

@method_decorator(
    name='get',
    decorator=swagger_auto_schema(
        operation_description="description from swagger_auto_schema via method_decorator"
    )
)
class ABC(generics.ListCreateApiView):
    serializer_class = MessageSerializer

    def get_queryset(self):
        data =self.request.GET.get("code")
        ...
    @swagger_auto_schema(
        operation_description="THIS API IS TO CREATE MESSAGES IN A LIST ",
        request_body=openapi.Schema(
            type=openapi.TYPE_OBJECT,
            required=["data"],
            properties={
                "code": openapi.Schema(type=openapi.TYPE_STRING),
            },
         )
     )
     def post(self, request):
         brand_code = request.data.get("code")
         ...

--------------
#serializer.py

class MessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Messages
        fields = ("message_id", "content", "description")
EDG956
  • 797
  • 8
  • 23
  • I tried this and it's not working, Is there any other possible way to do it – ast Sep 27 '22 at 04:43
  • @ast I realised that `ApiView`s do not define `list` or `create` actions (that's `ViewSet`s). Try now, using `get` instead of `list` in `method_decorator` – EDG956 Sep 27 '22 at 07:36