#14497 closed New feature (fixed)
ModelAdmin.readonly_fields isn't graceful with filefields.
Reported by: | Owned by: | Paul Collins | |
---|---|---|---|
Component: | contrib.admin | Version: | 1.2 |
Severity: | Normal | Keywords: | feature admin readonly filefield |
Cc: | kez.knight@…, adam@…, thepapermen, riccardo.magliocchetti@…, cmawebsite@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | yes |
Description
Currently, if supplying a FileField (and presumably, an ImageField or anything subclassing FileField) in the list of readonly_fields on a ModelAdmin instance, the value stored against the database (the path to its location on the filesystem or storage backend) is displayed as plain text.
It would be neater if instead, it displayed an HTML link of path, as it does when displaying the standard file upload widget in a non-readonly context (though excepting the file upload field itself). This would allow for more intuitive handling of admin access to read-only forms with additional data (eg: downloading uploaded PDFs, which is the use-case I'm facing at the moment.)
Looking at the source, I think the place to tackle this would be AdminReadonlyField.contents() in source:django/trunk/django/contrib/admin/helpers.py but that is just a cursory glance.
Obviously, low priority feature request, rather than a bug.
Attachments (4)
Change History (40)
comment:1 by , 14 years ago
comment:2 by , 14 years ago
Component: | Contrib apps → django.contrib.admin |
---|---|
Triage Stage: | Unreviewed → Accepted |
I can confirm it shows as plain text.
The URL problem dmoisset mentions would also apply to the widget used when it's not a readonly_field.
comment:4 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Cleanup/optimization |
comment:5 by , 14 years ago
Type: | Cleanup/optimization → New feature |
---|
comment:6 by , 14 years ago
Easy pickings: | unset |
---|---|
Owner: | changed from | to
Status: | new → assigned |
by , 14 years ago
Attachment: | admin-filefield-readonly-display.diff added |
---|
comment:7 by , 14 years ago
Cc: | added |
---|---|
Has patch: | set |
I have added a patch that outputs readonly FileField (and it's subclasses such as ImageField) values as links to the download file.
comment:8 by , 14 years ago
UI/UX: | set |
---|
comment:9 by , 14 years ago
Needs tests: | set |
---|
comment:11 by , 13 years ago
You might want to check out the admin's Selenium test cases found from django/contrib/admin/tests.py. Seems like the perfect fit for testing this.
comment:12 by , 13 years ago
Let me suggest a small adding. Usually is not necessary to show the complete path, but just the file name. This is trivial by "import os.path" and applying "os.path.basename" as below:
return mark_safe(u'<a href="%s">%s</a>' % (escape(value.url), os.path.basename(force_unicode(value))))
comment:13 by , 13 years ago
Needs tests: | unset |
---|---|
Owner: | changed from | to
Status: | assigned → new |
@adamjforster I'm grabbing this ticket since you called out for some help writing tests. Please feel free to grab it back if you'd like. I removed your check on for value.file, because it causes an IO exception (no such file) during the unittests. I figured that testing for the actual existence of the file wasn't really needed to generate the link to the file.
@akaariai I've added the Selenium tests, but I'm not sure if that's the fastest way to test that the field is rendering correctly in the admin. Suggestions?
@javiersanchez I can update the patch for this but that would make this particular interface inconsistent with the read/write interface. In the current version the link is rendered the same way, though it's rendered by different code.
comment:14 by , 13 years ago
After some discussion with Julien I removed the Selenium tests and did basically the same thing with the django.test.client
by , 13 years ago
Attachment: | admin-filefield-readonly-display_with_tests.diff added |
---|
comment:15 by , 13 years ago
Updated because julien was getting a unit test failure on his osx machine. Is there a bug with assertContains(html=True)?
If this passes on his machine then there might be an issue in there. More when I hear back from julien.
by , 13 years ago
Attachment: | 14497.admin-filewidget-readonly.diff added |
---|
follow-up: 17 comment:16 by , 13 years ago
Patch needs improvement: | set |
---|
Thank you, Paul. I realized the test was failing for me because of the hardcoded "media/" in the url path. Using the string variable STORAGE_URL
fixes that (see patch above).
I've also noticed that the same display logic is used on the changelist when a file field is in list_display
. It'd be great to write an extra test to cover that test. Other than that, the patch looks great!
comment:17 by , 13 years ago
Triage Stage: | Accepted → Design decision needed |
---|
Replying to julien:
I've also noticed that the same display logic is used on the changelist when a file field is in
list_display
.
Uhoh. So this is something I completely missed. It seems that display_for_field is implicitly expected to just return a text value, not an HTML value.
If a read only file field is listed in the list_display
then in the admin the link to edit that object vanishes. The HTML ends up looking like <a href='link to admin page'><a href='link to file'>filepath/filename</a></a>. Worse, if it's the only thing listed in list_display
it leaves the end user no obvious way to go and actually edit the object.
An icky feeling fix would be to go and put the special case logic for this in contrib.admin.helpers.AdminReadonlyField.contents which is what's being called to display it on the modify object page. After some discussion with julien though, I'm kicking this back to DDN. Maybe at some point the AdminReadonlyField class will be refactored to allow for this kind of flexibility.
comment:18 by , 13 years ago
Cc: | added |
---|
follow-up: 21 comment:20 by , 12 years ago
+1
Weird it isn't there, please include this in the next minor release
comment:22 by , 12 years ago
Triage Stage: | Design decision needed → Accepted |
---|
comment:23 by , 12 years ago
Cc: | added |
---|
comment:24 by , 11 years ago
@aaugustin
So I see you've marked this as accepted. I can dig back into this issue again and see if my horrible hack solution would even still apply here. Assuming it does is that the direction I should move this in?
comment:25 by , 11 years ago
I marked this ticket as "accepted" when we removed "design decision needed"; it seemed more appropriate than closing it. But I haven't looked at the patch.
by , 11 years ago
Attachment: | 14497-admin-filewidget-readonly.diff added |
---|
Updated to apply to a recent git master, also simplified a bit by reusing current widgetadmin instead of creating a new one
comment:27 by , 10 years ago
Submit a pull request on GitHub and uncheck "Patch needs improvement" so the ticket appears in the review queue. Then find a friend to review the patch and mark the ticket as "Ready for checkin".
comment:29 by , 10 years ago
Patch needs improvement: | unset |
---|
comment:30 by , 10 years ago
Cc: | added |
---|
comment:32 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:36 by , 10 years ago
I also discovered that this takes precedence over list_display_links, but I think I like the new behavior.
I think that an issue here is that the file stored in a filefield is not necessarily mapped to an URL (and even if it is, it's not obvious which URL)