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")我的post方法可以很好地处理使用相同序列化程序所需的字段,但它不适用于get_queryset方法。有人能建议我如何使用drf-yasg来获取字段吗?
发布于 2022-09-26 11:01:09
您不应该修饰get_queryset,因为它是一个内部函数,而不是ApiView端点的一部分。您可能会看到一个get请求,因为您使用的ListCreateApiView定义了get和post方法的处理程序。
由于您没有覆盖get方法处理程序,所以可以使用Django的method_decorator将一个装饰器注入到ApiView的get方法中,如drf-yasg关于swagger_auto_schema装饰器的部分所示。
下面是您的ApiView的一个示例实现,它也应该记录get端点。
@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")https://stackoverflow.com/questions/73850654
复制相似问题