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 Per Django documentation, "Internally, Django uses a django.core.files.File instance any time it needs to represent a file." This string ('bogus' in your case) is used as a 'name' attribut of that file, while fooinstance.pic.file has to return instance of File under that name. When you try to get fooinstance.pic.file, you receive '[Errno 2] No such file or directory', but otherwise any garbage could be stored without raising any validation errors, for both ImageField and FileField. I agree that it's strange. – Margo Grig Commented Jan 30 at 14:46
Add a comment  | 

1 Answer 1

Reset to default 0

the 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