Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FileUploadParser doesn't offer a file picker in the browsable API.#9646

Unanswered
jcoombes asked this question inPotential Issue
Discussion options

Problem: The browsable API doesn't interact well with the FileUploadParser.
Filling in the content field with the body of a CSV file doesn't help.

image
image

The view in question.

    @extend_schema(        description="Import project structure from JIRA-compatible CSV",        request={            "multipart/form-data": {                "type": "object",                "properties": {                    "file": {"type": "string", "format": "binary"},                },            },        },        responses={            201: OpenApiResponse(                description="Import successful with project data and URL",                response=ProjectSerializer,            ),            400: OpenApiResponse(description="Bad request - invalid file or format"),        },    )    @action(        detail=False,        name="import-csv",        parser_classes=[FileUploadParser],        renderer_classes=[JSONRenderer, BrowsableAPIRenderer],        serializer_class=CsvSerializer,        methods=["post"],    )    def import_csv(self, request: Request) -> Response:        """Import project structure from JIRA-compatible CSV."""        if "file" not in request.FILES:            return Response(                {"error": "No file uploaded"},                status=status.HTTP_400_BAD_REQUEST,            )        csv_file = request.FILES["file"]        if not csv_file.name.endswith(".csv"):            return Response(                {"error": "File must be a CSV"},                status=status.HTTP_400_BAD_REQUEST,            )        try:            assert isinstance(request.user, User)            decoded_file = io.StringIO(csv_file.read().decode("utf-8"))            (project, last_modified) = csv_io.import_jira(decoded_file, request.user)            serializer = self.get_serializer(project)            return Response(                serializer.data,                status=status.HTTP_201_CREATED,            )        except (UnicodeDecodeError, csv.Error) as e:            return Response(                {"error": f"Invalid CSV format: {e!s}"},                status=status.HTTP_400_BAD_REQUEST,            )
You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

Adding a serializer includes the file picker which solves for my use case; this might just be a documentation issue. The documentation does not mention the required serializer anywhere.
https://www.django-rest-framework.org/api-guide/parsers/#fileuploadparser

    @extend_schema(        description="Import project structure from JIRA-compatible CSV",        request={            "multipart/form-data": {                "type": "object",                "properties": {                    "file": {"type": "string", "format": "binary"},                },            },        },        responses={            201: OpenApiResponse(                description="Import successful with project data and URL",                response=ProjectSerializer,            ),            400: OpenApiResponse(description="Bad request - invalid file or format"),        },    )    @action(        detail=False,        name="import-csv",        serializer_class=CsvImportSerializer,        methods=["post"],    )    def import_csv(self, request: Request) -> Response:        """Import project structure from JIRA-compatible CSV."""        if "file" not in request.FILES:            return Response(                {"error": "No file uploaded"},                status=status.HTTP_400_BAD_REQUEST,            )        csv_file = request.FILES["file"]        if not csv_file.name.endswith(".csv"):            return Response(                {"error": "File must be a CSV"},                status=status.HTTP_400_BAD_REQUEST,            )        try:            assert isinstance(request.user, User)            decoded_file = io.StringIO(csv_file.read().decode("utf-8"))            (project, last_modified) = csv_io.import_jira(decoded_file, request.user)            output_serializer = ProjectSerializer(project, context={"request": request})            return Response(                output_serializer.data,                status=status.HTTP_201_CREATED,            )        except (UnicodeDecodeError, csv.Error) as e:            return Response(                {"error": f"Invalid CSV format: {e!s}"},                status=status.HTTP_400_BAD_REQUEST,            )
You must be logged in to vote
1 reply
@browniebroke
Comment options

The documentation does not mention the required serializer anywhere.
django-rest-framework.org/api-guide/parsers#fileuploadparser

That documentation page focuses purely on file uploads, especially theFileUploadParser class. It tries to provide a minimal example that works in the case of a programmatic request, ignoring the browsable API. introducing a serializer would be distracting the point of the section IMO.

We could however add a note at the end of the section to make this limitation clearer and point towards the solution (serializer with a FileField). Another potential page that may be suitable is the one about thebrowsable API, we could add a section that it infers form fields types from serializers and might be wrong if you don't use a serializer of if the serializer resolution is too dynamic...

PS: what was your originalCsvSerializer?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
2 participants
@jcoombes@browniebroke

[8]ページ先頭

©2009-2025 Movatter.jp