69👍
✅
from django.core.files import File
user1=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
directory
specified 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/
35👍
from django.core.files import File
user1=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.
- [Django]-Related Field got invalid lookup: icontains
- [Django]-Django Rest Framework custom authentication
- [Django]-How do I get the object if it exists, or None if it does not exist in Django?
1👍
What worked for me was:
django.core.files.uploadedfile import UploadedFile
user1=User(name='abc')
user1.pic.save('abc.png', UploadedFile(file=open('/tmp/pic.png', 'rb'), content_type='image/png'))
- [Django]-How to detect Browser type in Django?
- [Django]-Which Stack-Overflow style Markdown (WMD) JavaScript editor should we use?
- [Django]-Stack trace from manage.py runserver not appearing
Source:stackexchange.com