The ImageField docu states:
Inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image.
Yet any string is accepted
class Foo(Model):
pic = models.ImageField(upload_to='files')
e.g. I can save this without error and nothing is uploaded to files
(not even with a correct file)
fooinstance.pic="bogus"
fooinstance.save()
fooinstance.full_clean()
fooinstance.pic.__dict__
{'_file': None,
'name': 'bogus',
'instance': <Foo:...>,
'field': <django.db.models.fields.files.ImageField: pic>,
'storage': <django.core.files.storage.filesystem.FileSystemStorage at 0x721add4903d0>,
'_committed': True}
Meanwhile the FileField works/uploads perfectly fine
The ImageField docu states:
Inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image.
Yet any string is accepted
class Foo(Model):
pic = models.ImageField(upload_to='files')
e.g. I can save this without error and nothing is uploaded to files
(not even with a correct file)
fooinstance.pic="bogus"
fooinstance.save()
fooinstance.full_clean()
fooinstance.pic.__dict__
{'_file': None,
'name': 'bogus',
'instance': <Foo:...>,
'field': <django.db.models.fields.files.ImageField: pic>,
'storage': <django.core.files.storage.filesystem.FileSystemStorage at 0x721add4903d0>,
'_committed': True}
Meanwhile the FileField works/uploads perfectly fine
Share Improve this question edited Jan 30 at 4:44 jjk asked Jan 29 at 21:31 jjkjjk 5952 gold badges11 silver badges31 bronze badges 1 |1 Answer
Reset to default 0the uploaded object (eg. when used in a form).
You're just assigning a string (path-in-storage) to the field, and not even calling full_clean
on the model.
'name'
attribut of that file, whilefooinstance.pic.file
has to return instance of File under that name. When you try to getfooinstance.pic.file
, you receive'[Errno 2] No such file or directory'
, but otherwise any garbage could be stored without raising any validation errors, for bothImageField
andFileField
. I agree that it's strange. – Margo Grig Commented Jan 30 at 14:46