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:
parent
f5723de9fd
commit
0f888da376
1 changed files with 2 additions and 2 deletions
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue