Opened 112 minutes ago

Last modified 103 minutes ago

#36622 new Bug

FileField.__init__ is triggering storage LazyObject resolution at boot time

Reported by: Fabien MICHEL Owned by:
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords:
Cc: Fabien MICHEL Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Fabien MICHEL)

There is this code in FileField.__init__ :

self.storage = storage or default_storage

The problem is that if we want to have a LazyObject as storage for the field, it is still resolved at boot time, and it seems unecessary to trigger resolution at this moment. This seems not expected as the documentation itself suggest to use LazyObject in case we require a callable to be overridable during tests : https://docs.djangoproject.com/en/5.2/topics/files/#using-a-callable

Using storage or default_storage is calling storage.__bool__ which trigger resolution on default LazyObject.

A solution would be to change this line by :

self.storage = storage if storage is not None else default_storage

A workaround for the case specified in the documentation is to override __bool__ on LazyObject so it always returns True.

class OtherStorage(LazyObject):
    def _setup(self):
        self._wrapped = storages["mystorage"]

    def __bool__(self):
        return True

Change History (3)

comment:1 by Fabien MICHEL, 111 minutes ago

Type: UncategorizedBug

comment:2 by Fabien MICHEL, 109 minutes ago

Description: modified (diff)

comment:3 by Sarah Boyce, 103 minutes ago

Related #36504

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