#35657: Specifying db_default on FileField crashes
-------------------------------------+-------------------------------------
Reporter: David Sanders | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.0 | Severity: Normal
Keywords: db_default | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
I guess there's the existential question: "Does db_default make sense with
FileField/ImageField?" Perhaps, as long as there's a file at the path
defined by the `db_default` expression? 🤔
In any case when defined with a `FileField`, the presence of
`DatabaseDefault` as the field's value causes a crash:
Given the model:
{{{
class Foo(models.Model):
bar = models.FileField(db_default="path/to/file.txt")
}}}
We get `AttributeError` exceptions when attempting to create an instance:
{{{
>>> Foo.objects.create()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/path/to/django/django/db/models/manager.py", line 87, in
manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/query.py", line 660, in create
obj.save(force_insert=True, using=self.db)
File "/path/to/django/django/db/models/base.py", line 891, in save
self.save_base(
File "/path/to/django/django/db/models/base.py", line 997, in save_base
updated = self._save_table(
^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/base.py", line 1160, in
_save_table
results = self._do_insert(
^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/base.py", line 1201, in
_do_insert
return manager._insert(
^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/manager.py", line 87, in
manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/query.py", line 1828, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/sql/compiler.py", line 1847, in
execute_sql
for sql, params in self.as_sql():
^^^^^^^^^^^^^
File "/path/to/django/django/db/models/sql/compiler.py", line 1770, in
as_sql
value_rows = [
^
File "/path/to/django/django/db/models/sql/compiler.py", line 1771, in
<listcomp>
[
File "/path/to/django/django/db/models/sql/compiler.py", line 1772, in
<listcomp>
self.prepare_value(field, self.pre_save_val(field, obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/sql/compiler.py", line 1720, in
pre_save_val
return field.pre_save(obj, add=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/django/django/db/models/fields/files.py", line 320, in
pre_save
if
file.name is None and file._file is not None:
^^^^^^^^^
AttributeError: 'DatabaseDefault' object has no attribute 'name'
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/35657>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.