Settings upload_to at project level settings

I’d like to create a handler for upload_to that uses “model_name/id/original_filename” across all models.

Is there a way to do so using Currently, I’m including it manually with every field. Is there a way to set it globally like we do for STORAGES?

No, there is no global override for upload_to.

You could create a custom FileField class that defines a function referenced by the upload_to attribute, and define your models to use your custom class instead FileField. (You could even make that function a reference to a setting if you want it globally-adjustable at run time.)

Or, you could possibly create a custom Storage class and override the generate_filename method in it.

Thanks for the response, a custom storage class would not work as I’m already leveraging a third-party library and the onus remains on the team to keep the extension updated in the long term.

That would be a roundabout way to do it since we already have upload_to - an argument specifically for modifying the filename or path on top of the storage’s default.

I’ll stick to defining the callable manually for the moment. I have raised a ticket for this to be a new feature though (since in my mind, it’s no different from the storage callable or the handler400 settings, etc)

This is one of those things that could be tested as a third-party package prior to being implemented in core. I think it would be worth implementing independently at first to see how it works out.

1 Like