GET
метод не может транспортировать какие-либо данные контента, поэтому вам нужно использовать POST
метод:
$.ajax({
type: 'POST',
...
}
Теперь внутри вашего view
вы можете получить файлоподобный объект изrequest.FILES
:
def upload_videos(request):
video_file = request.FILES['file']
# Create model record
_ = mymodel.objects.create(myfile=video_file.getvalue())
return Httpresponse('saved')
OTOH, если вы получаете data
как POST
параметр, вы можете использовать io.BytesIO
, чтобы получить файлоподобный объект, и использовать его для создания записи модели:
import io
def upload_videos(request):
try:
video_file = request.FILES['file']
except KeyError:
# You can catch KeyError here as well
# and return a response with 400 status code
try:
video_file_data = request.POST['data']
except KeyError:
return Httpresponse('No file content', status=400)
video_file = io.BytesIO(video_file_data)
# Create model record
_ = mymodel.objects.create(myfile=video_file.getvalue())
else:
# Create model record
_ = mymodel.objects.create(myfile=video_file.getvalue())
return Httpresponse('saved')
FWIW, вы должны назвать свои классы моделей как CamelCase .
Выше приведена основная идея, как это сделать, вы можете улучшить идеюудовлетворить ваши потребности.