jazzband / django-polymorphic

Improved Django model inheritance with automatic downcasting
https://django-polymorphic.readthedocs.io
Other
1.66k stars 282 forks source link

Polymorphic inline form ignoring multipart for FileFields from Django 2.1 #380

Open damijanracel opened 5 years ago

damijanracel commented 5 years ago

Admin form with polymorphic inlines is not multipart when you use an ImageField or a FileField, which results in the files not being uploaded to the server.

This issue can be replicated by implementing the example from documentation with StackedPolymorphicInline for django admin and having FileField or ImageField in the polymorphic models.

Polymorphic Inline documentation

Using the identical example on versions of django prior to 2.1, works ok.

atleta commented 4 years ago

I've just debugged this issue not understanding how file upload could have worked in an earlier version of our app. The problem is that the django admin code tries to detect if there is a FileField or ImageField (or, for that matter, any Field that needs a multipart form) in the Form and if so, then the form is generated with the multipart enctype.

There is a quick and dirty workaround for this, though: define a form for the parent admin (i.e. the belonging to the polymorphic base class) and implement the is_multipart() method on that (simply just return True). Or you can also override the admin form templates in the django/contrib/admin/templates/admin/ directory (e.g. change_form.html) and make the form always multipart (as it was with 2.0 and before).

Looking at the 2.0 source you can see why it used to work before: https://github.com/django/django/blob/b99221b8bb2a4cd7fd5ac6e5f03e8eb1953d6384/django/contrib/admin/options.py#L1064