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.
This commit is contained in:
Kumi 2024-03-16 21:46:00 +01:00
parent f5723de9fd
commit 0f888da376
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -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: