Open mcsimps2 opened 3 years ago
I want to display the full path of uploaded files like this:
"commercial_proposal": { "old": "http://localhost:8000/submissions/commercial/tender_fAcfkpf.doc", "new": "http://localhost:8000/submissions/commercial/programme_backend_gaulGQh.pdf" },
"commercial_proposal": { "old": "submissions/commercial/tender_fAcfkpf.doc", "new": "submissions/commercial/programme_backend_gaulGQh.pdf" },
Problem In my application, I have a use case where I do not want to transform a FileField/ImageField to a TextField/CharField in a historical model. For developers who want to access the URLs of files and images in historical records without a hassle, maintaining the underlying type of a FileField is very useful. Note that Django, or at least the most recent version, does not delete files when a referring row is deleted, which is great for historical records (unless you want to clean up dangling files).
E.g. I cannot write
user.history.most_recent().profile_picture.url
with the current codebase. I have to manually resolveuser.history.most_recent().profile_picture
to a URL myself.Solution I've forked off this codebase with a simple modification. In
models.py
, I added a settingSIMPLE_HISTORY_TRANFORM_FILEFIELD
, which defaults toTrue
. In my use case, I set this toFalse
so that I maintain ImageFields/FileFields as I need to:This has worked well for me without a problem, even as records are updated and deleted. I'm always able to access the historical image/file URL as I want to. However, I am interested if anyone familiar with the codebase foresees logical problems with this solution that could arise, which may explain why this option was never offered in the first place.
Alternatives A manual alternative to this is that developers can manually resolve the CharFields to files. This is a bit of a hassle, because with file fields, you can call
user.profile_picture.url
. In the case of a historical model, you would need to resolveuser.profile_picture
to a URL using Django storage backends andFile
subclasses.I would be glad to open a PR if others think this would be a useful addition.