[Django] #25528: `FileField(null=True)` doesn't work as expected

1 view
Skip to first unread message

Django

unread,
Oct 8, 2015, 12:11:55 AM10/8/15
to django-...@googlegroups.com
#25528: `FileField(null=True)` doesn't work as expected
-------------------------------------+-------------------------------------
Reporter: mrmachine | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.8
(models, ORM) | Keywords: FileField null empty
Severity: Normal | string
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/25528>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 8, 2015, 6:19:06 AM10/8/15
to django-...@googlegroups.com
#25528: `FileField(null=True)` doesn't work as expected
-------------------------------------+-------------------------------------
Reporter: mrmachine | Owner: nobody
Type: Uncategorized | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: FileField null | Triage Stage:
empty string | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by claudep):

* status: new => closed
* needs_docs: => 0
* resolution: => duplicate
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

Duplicate of #10244

--
Ticket URL: <https://code.djangoproject.com/ticket/25528#comment:1>

Reply all
Reply to author
Forward
0 new messages