Saving a file uploaded using ajax to the Django ImageField model

The file is available as raw data in the request. How should I convert it to save to disk represented by ImageField inside a model .

I tried:

 file_ = File(request) modelinstance.picture.save(filename, file_, save=False) modelinstance.save() 

and some options above. An example of how to do this in a django view will help.

Thanks!

+4
source share
2 answers

Solved it like this:

 filename = request.GET[ 'yourfilename' ] from django.core.files.uploadedfile import SimpleUploadedFile file_contents = SimpleUploadedFile("%s" %(filename), request.raw_post_data) modelinstance.picture.save(filename, file_contents, True) 
+1
source

If you used ModelForm , you would not have anything special. ModelForm automatically processes FileField / ImageField.

If you really want to do this manually, django has documents

+1
source

Source: https://habr.com/ru/post/1441304/


All Articles