Получить 'str' в Djano forms.FileField()

Я загружаю без формы. У моего пользователя есть поле FileField, если я загружаю через <input type...> у меня нет проблем. Но это не так удобно для моей цели.

views.py

    def uploadaction2(request):
        f = forms.FileField() # this is my idea
        data = json.loads(request.body)
        # f.clean("data['data']") # error: file_name = data.name AttributeError: 'str' object has no attribute 'name'
        logger.error(type(data['data'])) #gives <class 'str'> and without the type the string that i want to save
        with open('newfile', 'w') as file:
            file.write(data['data'])
        request.user.file = file 
        request.user.save() # i get no error until this part

Ошибка

Internal Server Error: ....  in pre_save
    if file and not file._committed:
AttributeError: '_io.TextIOWrapper' object has no attribute '_committed'

Я думаю, что мне нужно преобразовать строку json в forms.FileField(), но я понятия не имею, как это можно сделать.

    def uploadaction2(request):
        file = request.FILES.get('file') # set name to input
        request.user.file = file
        request.user.save()
Вернуться на верх