Non image file can be saved to ImageField
|Reported by:||johnsmith||Owned by:||deprince|
|Component:||Database layer (models, ORM)||Version:||1.4|
|Cc:||niwi@…, deprince@…||Triage Stage:||Accepted|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image.
I was trying to find out the exact exception that would be raised if a file is not an image and it appears that no such image validation is done. So I did a test, with a model such as this
class Image(models.Model): caption = models.Charfield(max_length=60) image = models.ImageField(upload_to='somewhere')
I tried this
f = django.core.files.base.ContentFile('not an image') i = myapp.models.Image() i.caption("This should throw an error but doesn't") i.image.save('bar.jpg', f) i.save()
I don't get any errors and the file is saved. Maybe I'm doing something wrong but from what the docs say I'm expecting to be prevented from doing this.
From pip freeze