Skip to content

Commit

Permalink
Fixed #35657 -- Made FileField handle db_default values.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahboyce authored Aug 5, 2024
1 parent e9e1470 commit 8deb6bb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions django/db/models/fields/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.core.files.storage import Storage, default_storage
from django.core.files.utils import validate_file_name
from django.db.models import signals
from django.db.models.expressions import DatabaseDefault
from django.db.models.fields import Field
from django.db.models.query_utils import DeferredAttribute
from django.db.models.utils import AltersData
Expand Down Expand Up @@ -197,6 +198,12 @@ def __get__(self, instance, cls=None):
attr = self.field.attr_class(instance, self.field, file)
instance.__dict__[self.field.attname] = attr

# If this value is a DatabaseDefault, initialize the attribute class
# for this field with its db_default value.
elif isinstance(file, DatabaseDefault):
attr = self.field.attr_class(instance, self.field, self.field.db_default)
instance.__dict__[self.field.attname] = attr

# Other types of files may be assigned as well, but they need to have
# the FieldFile interface added to them. Thus, we wrap any other type of
# File inside a FieldFile (well, the field's attr_class, which is
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/5.0.8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ Bugfixes
* Fixed a bug in Django 5.0 which caused constraint validation to either crash
or incorrectly raise validation errors for constraints referring to fields
using ``Field.db_default`` (:ticket:`35638`).

* Fixed a crash in Django 5.0 when saving a model containing a ``FileField``
with a ``db_default`` set (:ticket:`35657`).
3 changes: 3 additions & 0 deletions tests/file_storage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def pathlib_upload_to(self, filename):
default = models.FileField(
storage=temp_storage, upload_to="tests", default="tests/default.txt"
)
db_default = models.FileField(
storage=temp_storage, upload_to="tests", db_default="tests/db_default.txt"
)
empty = models.FileField(storage=temp_storage)
limited_length = models.FileField(
storage=temp_storage, upload_to="tests", max_length=20
Expand Down
14 changes: 14 additions & 0 deletions tests/file_storage/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,20 @@ def test_filefield_default(self):
self.assertEqual(obj.default.read(), b"default content")
obj.default.close()

def test_filefield_db_default(self):
temp_storage.save("tests/db_default.txt", ContentFile("default content"))
obj = Storage.objects.create()
self.assertEqual(obj.db_default.name, "tests/db_default.txt")
self.assertEqual(obj.db_default.read(), b"default content")
obj.db_default.close()

# File is not deleted, even if there are no more objects using it.
obj.delete()
s = Storage()
self.assertEqual(s.db_default.name, "tests/db_default.txt")
self.assertEqual(s.db_default.read(), b"default content")
s.db_default.close()

def test_empty_upload_to(self):
# upload_to can be empty, meaning it does not use subdirectory.
obj = Storage()
Expand Down

0 comments on commit 8deb6bb

Please sign in to comment.