Opened 9 years ago

Closed 9 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.

Change History (1)

comment:1 by Claude Paroz, 9 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #10244

Note: See TracTickets for help on using tickets.
Back to Top