I have separate API endpoint to upload files to AWS S3 storage.
class TaskFilesUploadView(generics.GenericAPIView):
"""
APIView for uploading task files into cloud.
"""
parser_classes = (MultiPartParser,)
serializer_class = TaskFileUploadSerializer
def post(self, request, *args, **kwargs):
file_obj = request.FILES["file"]
file_directory_within_bucket = "task_files"
file_path_within_bucket = os.path.join(file_directory_within_bucket, file_obj.name)
default_storage.save(file_path_within_bucket, file_obj)
file_url = default_storage.url(file_path_within_bucket)
return Response(
{
"message": "File uploaded successfully",
"file_url": file_url
},
status=status.HTTP_200_OK
)
I have a Task model where a single instance can have multiple files.
class TaskV1(models.Model):
"""
Task table model.
"""
...
class TaskFileV1(models.Model):
"""
Since task can have several files in one task,
we need this table with FK to TaskV1.
"""
task = models.ForeignKey(TaskV1, on_delete=models.CASCADE, related_name="files")
file = models.FileField()
During task creation client attaches files and then pushes "save task" button, so i need to pass recieved file urls into json.
{
...
"files": [
{
"file": "string"
}
]
}
But i got HTTP 400 BAD REQUEST with following error:
The submitted data was not a file. Check the encoding type on the form.
How should it be implemented?