From 0f888da3768671eca618bcbfce00da90c6f0cf8b Mon Sep 17 00:00:00 2001 From: Kumi Date: Sat, 16 Mar 2024 21:46:00 +0100 Subject: [PATCH] feat: assign category to media post-serialization Changed the approach to assigning a category to an uploaded media file by fetching the Category instance from the database after the media file has been serialized and saved. This ensures that the category assigned is validated against existing categories in the database, enhancing data integrity and error handling when an incorrect category is specified. Previously, the assignment used a direct extraction from request arguments which might not have validated the category's existence. --- quackscape/users/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quackscape/users/views.py b/quackscape/users/views.py index 6dffba1..771a3d4 100644 --- a/quackscape/users/views.py +++ b/quackscape/users/views.py @@ -86,9 +86,9 @@ class FileUploadView(GenericAPIView): serializer = serializer_class(data=request.data) if serializer.is_valid(): - instance.category = request.kwargs["category"] instance = serializer.save() - instance.refresh_from_db() + instance.category = Category.objects.get(id=kwargs["category"]) + instance.save() return Response(OriginalMediaSerializer(instance).data, status=status.HTTP_201_CREATED) else: