#34540 closed New feature (wontfix)

[feature request] Run FileField storage callable during runtime and not during buildtime

Reported by: Vasanth Owned by: nobody
Component: File uploads/storage Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Vasanth)

If the storage callable can be invoked during runtime similar to the "upload_to", it can allow us to choose the storage to use dynamically. This is particularly helpful for SaaS services which prefer maintaining independent storages per client/subdomain.

Something like, the following snippet where local_state.tenant is threading local() which holds the name of the tenant for each request

def tenant_storage(instance, filename):
    tenant = getattr(local_state, "tenant", "default")
    return storages[tenant]

Change History (5)

comment:1 by Vasanth, 12 months ago

Description: modified (diff)

comment:2 by Natalia Bidart, 12 months ago

Triage Stage: UnreviewedAccepted
Version: 4.2dev

Accepting since I agree that there is no need to evaluate the callable at field creation time, and that there is value in having consistency between upload_to and storage.

comment:3 by Natalia Bidart, 12 months ago

Actually on a second thought, this may be best decided in the forum to have a wider audience. Posting there!

comment:4 by Mariusz Felisiak, 12 months ago

Also, this is a documented behavior so we cannot change it without the deprecation path:

"Your callable will be evaluated when your models classes are loaded, and must return an instance of Storage."

Version 0, edited 12 months ago by Mariusz Felisiak (next)

in reply to:  4 comment:5 by Natalia Bidart, 12 months ago

Resolution: wontfix
Status: newclosed

Replying to Mariusz Felisiak:

Also, this is a documented behavior so we cannot change it without the deprecation path:

"Your callable will be evaluated when your models classes are loaded, and must return an instance of Storage."

-1 from me.

Good point, I'll close for now as wontfix, then if there is enough traction in the forum we can revisit. Thanks!

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