admin管理员组文章数量:1315958
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.
本文标签: pythonWhat is meant with validation for django imagefieldStack Overflow
版权声明:本文标题:python - What is meant with validation for django imagefield? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741995766a2410012.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'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