-
Notifications
You must be signed in to change notification settings - Fork 272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
no way to specify parser_classes for views in a viewset #266
Comments
there is class XViewset(viewsets.ModelViewSet):
serializer_class = M3Serializer
queryset = M3.objects.none()
parser_classes = [parsers.MultiPartParser]
@action(methods=['POST'], detail=False, parser_classes=[parsers.JSONParser])
def json_only_action(self, request):
pass this will get properly detected. for requests we have this: @extend_schema(responses={
(200, 'application/json'): OpenApiTypes.OBJECT,
(200, 'application/pdf'): OpenApiTypes.BINARY,
}) something similar could be constructed for requests. however, |
Hello, I have the same issue as OP (I would like to set multipart/form-data for only the 'create' method on ModelViewSet). I can set the MultiPartParser in the parser_classes or by redefining 'get_parsers' and it works great, but changes are not reflected in the Swagger. Now, there may be an option to redefine this but I have yet to find it. EDIT: Typo |
so there is no way you can dynamically adapt the parser. there is only with the second part I meant we might add an the other fix improves the method case and I would say that this now models exactly how DRF behaves. @podplatnikm @NANASHI0X74 please test this. i consider this issue resolved with the added feature and improved detection. |
oooh nice 👍 |
I totally forgot about this yesterday... I'll try it next week though now, because I got an urgent task for this week today |
looks like the schema generated is correct now- @extend_schema_view(
create=extend_schema(request={'multipart/form-data': DocumentEditSerializer})
)
class DocumentViewSet(viewsets.ModelViewSet): And the schema generated is: /documents/
post:
operationId: documents_create
description: ''
tags:
- documents
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/DocumentEditRequest'
DocumentEditRequest:
type: object
properties:
creator:
type: integer
nullable: true
company:
type: integer
nullable: true
name:
type: string
maxLength: 255
description:
type: string
nullable: true
maxLength: 255
filepath:
type: string
format: binary
nullable: true |
Describe the bug
I've got a DocumentViewset that's used to both upload and retrieve files. For the create and update actions, the only sensible parser is MultiPartParser, but all other actions should accept json. So I don't want to set parser_classes on the whole viewset and it looks like there is no such thing as get_parser_classes akin to what can be used for serializers in drf.
There is an annotation in drf that can be used for function based views to override parser_classes, but that won't work for single views in viewsets afaict.
Maybe in this case it would make sense to have an override for parser_classes in extend_schema just like for request/response serializers?
An example:
The resulting schema for the above includes all three of the default parsers:
The text was updated successfully, but these errors were encountered: