I am trying to save an image file through django shell.
My model.py
is:
class user(models.Model):
name=models.CharField(max_length=20)
pic=models.ImageField()
Everyhing is fine with admin and forms but I want to save image using shell:
something like
>>>user1=User(name='abc', pic="what to write here")
You will end up with the image
abc.png
copied into theupload_to
directory specified in theImageField
.In this case, the
user1.pic.save
method will also save theuser1
instance. The documentation for saving anImageField
can be found here https://docs.djangoproject.com/en/dev/ref/files/file/Please Use 'rb' instead of 'r'. If you are using python3.