Opened 10 years ago
Closed 10 years ago
#25528 closed Uncategorized (duplicate)
`FileField(null=True)` doesn't work as expected
| Reported by: | Tai Lee | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.8 |
| Severity: | Normal | Keywords: | FileField null empty string |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Given this model:
class Foo(models.Model):
foo_file = models.FileField(null=True)
which allows null values, if I create a foo object, the foo_file field appears to be None by default, as I would expect. But on save, it is converted to an empty string:
>>> f = Foo() >>> f.foo_file.name == '' False >>> f.foo_file.name == None True >>> f.save() >>> f = Foo.objects.get() >>> f.foo_file == '' True
This is both unexpected, and also makes null=True a bit pointless for FileField.
If it is really by design that file fields cannot be null, setting null=True on the field should be an error and the field should have default='' by default. Although I think it would be better for FileField(null=True) to work more like other fields.
Duplicate of #10244