Opened 4 years ago
Closed 4 years ago
#32464 closed New feature (wontfix)
Enable "detaching" of file from FileField and ImageField
Reported by: | Samuli-Petrus Korhonen | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 3.1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When model contains FileField (or ImageField) cloning an instance by setting primarykey to None and saving object leads to problems. It seems that the resulting objects refer to same file which is not the desired behaviour as this leads to invalid objects.
It would make sense to have facility to "detach" FileField from file without deleting the actual file. After taking a look at the FieldFile code I think this functionality would be trivial to add and I would be happy to submit a pull request implementing this feature if it has sufficient merit to be merged.
Also, I would like to have feedback on whether maintainers prefer a separate "detach" function or if it should be implemented as parameter on "delete".
PS. I could also implement "attach" function if there is need for it.
Hi.
This strikes me as a special case of a much harder problem — you're going to get the same issue come up for any reference the model might hold. The copying model instances docs already hint at the issue: more-or-less you need to reset (or provide your own logic for) any fields for which the naive Use the same value isn't appropriate.
See #4027 for discussion about not including such more complex logic in Django.
I think there have been various takes on this issue over the years. django-clone looks to be one that is currently maintained.
I'm closing as
wontfix
but if you wanted to see if anyone on the DevelopersMailingList thinks this could be do-able, that would be OK. The conclusion from #4027 was roughly that per-project logic is relatively simple, but that a generic solution is almost impossible, so that it's not worth the complexity in core, but maybe that can be reviewed.Thanks.