From 8deb6bb1fc427762d56646bf7306cbd11fb5bb68 Mon Sep 17 00:00:00 2001 From: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> Date: Mon, 5 Aug 2024 21:36:49 +0200 Subject: Fixed #35657 -- Made FileField handle db_default values. --- django/db/models/fields/files.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'django/db/models/fields') diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py index 7c911f4b23..0716d3599e 100644 --- a/django/db/models/fields/files.py +++ b/django/db/models/fields/files.py @@ -9,6 +9,7 @@ from django.core.files.images import ImageFile 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 @@ -197,6 +198,12 @@ class FileDescriptor(DeferredAttribute): 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 -- cgit v1.3