Someone even requested the ability to have the instance passed into the
callable: https://github.com/django/django/pull/8477/files#r226555624
But I believe @miigotu misinterpreted it and was thinking about a
`Storage` instance.
What could be great is the ability to pass the instance (and maybe the
filename too) similar to
[https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.FileField.upload_to/
upload_to]
Example:
{{{
def get_storage(instance, filename):
if instance.is_private:
return PrivateStorage()
else:
...
}}}
This could allow the user to use a specific storage depending on the
instance being saved.
--
Ticket URL: <https://code.djangoproject.com/ticket/33993>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> A while ago [https://code.djangoproject.com/ticket/28184/ #28184]
> introduced the ability to use callables as the `storage` parameter of the
> `FileField`.
>
> Someone even requested the ability to have the instance passed into the
> callable: https://github.com/django/django/pull/8477/files#r226555624
>
> But I believe @miigotu misinterpreted it and was thinking about a
> `Storage` instance.
>
> What could be great is the ability to pass the instance (and maybe the
> filename too) similar to
> [https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.FileField.upload_to/
> upload_to]
>
> Example:
>
> {{{
> def get_storage(instance, filename):
> if instance.is_private:
> return PrivateStorage()
> else:
> ...
> }}}
>
> This could allow the user to use a specific storage depending on the
> instance being saved.
New description:
A while ago [https://code.djangoproject.com/ticket/28184/ #28184]
introduced the ability to use callables as the `storage` parameter of the
`FileField`.
Someone even requested the ability to have the instance passed into the
callable: https://github.com/django/django/pull/8477/files#r226555624
But I believe @miigotu misinterpreted it and was thinking about a
`Storage` instance.
What could be great is the ability to pass the instance (and maybe the
filename too) similar to
[https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.FileField.upload_to/
upload_to]
Example:
{{{
#!python
def get_storage(instance, filename):
if instance.is_private:
return PrivateStorage()
else:
...
}}}
This could allow the user to use a specific storage depending on the
instance being saved.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/33993#comment:1>
* status: new => closed
* resolution: => needsinfo
Comment:
This looks likely feasible for the `generate_filename` usage, but I can't
see immediately whether there'd be issues with `path` and `url` and so on.
(After file creation, we get the filename from the storage, so we'd need
to handle not having that available.)
I'd like to say that if you can put together a proof-of-concept PR, that
doesn't break anything, and isn't too complex then we can move forward,
but I think seeing that would be good before just accepting.
Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/33993#comment:2>
Comment (by Viicos):
Replying to [comment:2 Carlton Gibson]:
> This looks likely feasible for the `generate_filename` usage, but I
can't see immediately whether there'd be issues with `path` and `url` and
so on. (After file creation, we get the filename from the storage, so we'd
need to handle not having that available.)
Isn't this already the case now, as you can use a callable to get the
storage object? What I was suggesting is having the ability to pass the
instance (and additionally the filename) to this callable.
--
Ticket URL: <https://code.djangoproject.com/ticket/33993#comment:3>
Comment (by Carlton Gibson):
Currently, the storage is instantiated at model definition time (when the
field is instantiated as the model class definition is processed). There
is no instance at that time. So, there would need to be on-access update
(via a property, likely) to add the instance info. (How handling access
without an instance would need to be looked at.)
The same goes for the filename. Is it always available? What complexities
would need to be handled if not? 🤔
None of that's to say it couldn't work, but a proof-of-concept showing it
would help.
I hope that makes sense.
--
Ticket URL: <https://code.djangoproject.com/ticket/33993#comment:4>