Opened 7 years ago
Closed 7 years ago
#28585 closed Cleanup/optimization (fixed)
Set `has_file_field` context attribute for admin change forms conditionally on Form.is_multipart()
Reported by: | Mark Rogaski | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In django.contrib.admin.options.ModelAdmin:
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None): ... context.update({ ... 'has_file_field': True, # FIXME - this should check if form or formsets have a FileField, ...
This attribute is used to determine whether a multipart/form-data
is used for the form content type instead of application/x-www-form-urlencoded
.
I'd like to implement this behavior as it appears to have been intended (and I have a situation where multipart forms should only be used if necessary for file uploads). However, it's likely that some applications might be adversely impacted if the behavior changes. Perhaps adding an AVOID_MULTIPART_ADMIN_FORMS
setting or an avoid_multipart ModelAdmin
attribute that defaults to the current behavior is appropriate (better suggestions for the naming are welcome).
In any case, I'd like to be able to have non-multipart forms returned when multipart isn't needed without having to subclass ModelAdmin.
I am also happy to submit a PR for this based upon the preferred approach to backward compatibility.
Change History (6)
comment:1 by , 7 years ago
Summary: | Set `has_file_field` context attribute for admin change forms conditionally on form `is_multipart` method → Set `has_file_field` context attribute for admin change forms conditionally on Form.is_multipart() |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | New feature → Cleanup/optimization |
comment:2 by , 7 years ago
I used Form.is_multipart()
on an instantiated form in a mixin and that worked fine, but it doesn't appear to recognize file fields in any inlines.
comment:3 by , 7 years ago
Perhaps my comment implied that checking 'has_file_field': form.is_multipart()
is sufficient. I think the check for has_file_field
will also have to check is_multipart()
for the forms on the inline formset.
comment:4 by , 7 years ago
I'll put together a patch. I'd looked at it earlier and got stuck in the weeds a bit with the form introspection, but I'll look at it again over the weekend.
I think the check can reasonably use
Form.is_multipart()
and expected users to provide a custom form with that method overridden if needed. Do you have a case in mind where this isn't sufficient?