My problem is that when I try to save image to my model using Django shell I get this error that I can't find solution anywere.
models.py
class AdImage(models.Model):
ad = models.ForeignKey(Ad)
full_photo = models.ImageField(upload_to='uploads/', blank=True)
I import models create AdImage instance add 'ad' and try to
imagead.full_photo.save("NowHiring.jpg",open("C:\\NowHiring.jpg", "rb"))
but i get an error
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\hp\Envs\platform\lib\site-packages\django\db\models\fields\file
s.py", line 106, in save
self._size = content.size
AttributeError: '_io.BufferedReader' object has no attribute 'size'
Using: Python 3.5, Django 1.9
What could I do ?
The FieldFile.save
method needs to be called with an instance of django.core.files.File
, rather than a built-in python file handle. Change the save invocation to:
from django.core.files import File
imagead.full_photo.save("NowHiring.jpg", File(open("C:\\NowHiring.jpg", "rb")))
Django docs reference for FieldFile.save
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With