Saving image/file through django shell Saving image/file through django shell python python

Saving image/file through django shell


from django.core.files import Fileuser1=User(name='abc')user1.pic.save('abc.png', File(open('/tmp/pic.png', 'r')))

You will end up with the image abc.png copied into the upload_to directoryspecified in the ImageField.

In this case, the user1.pic.save method will also save the user1 instance.The documentation for saving an ImageField can be found here https://docs.djangoproject.com/en/dev/ref/files/file/


from django.core.files import Fileuser1=User(name='abc')user1.pic.save('abc.png', File(open('/tmp/pic.png', 'rb')))

Please Use 'rb' instead of 'r'. If you are using python3.


What worked for me was:

django.core.files.uploadedfile import UploadedFileuser1=User(name='abc')user1.pic.save('abc.png', UploadedFile(file=open('/tmp/pic.png', 'rb'), content_type='image/png'))