ImageField / FileField in the Wagtail admin

962 views
Skip to first unread message

Crew sCam

unread,
Feb 24, 2016, 8:01:50 AM2/24/16
to Wagtail support
Hello there!

So, we have a use case for having some images and files that are managed outside of the main Wagtail images / docs workflow.

I've tried adding ImageFields and FileFields to models registered as snippets and also to custom blocks, and while the initial widgets render OK, hitting save or publish does not seem to store any updated files.

I could potentially wire up my own custom field type that would implement similar functionality to the standard Django ones, but I'd obviously rather utilise the standard ones if possible.

Does anyone know why these fields' save functions are not getting executed in the 'wrapped' Wagtail versions?

Cheers, sCam

Matthew Westcott

unread,
Feb 24, 2016, 8:06:36 AM2/24/16
to wag...@googlegroups.com
Hi,
FileFields not working from snippets is a known issue - we need to set enctype="multipart/form-data" on the form when necessary. There's an incomplete pull request for it here: https://github.com/torchbox/wagtail/pull/758

Cheers,
- Matt

Crew sCam

unread,
Feb 24, 2016, 9:24:58 AM2/24/16
to Wagtail support
Ah! Great - thank you - my snippets are now saving FileFields correctly.

My next question is whether you think it's possible to get this working for custom blocks inside a StreamField?

I've created a custom BasicImageBlock that extends FieldBlock - this has an ImageField specified as its field. For testing purposes I've co-erced the page to have the multipart setting by adding a FileField to the page itself. When saving the page I got an 'InMemoryUploadedFile: x.jpeg is not JSON serializable' error. If I override get_prep_value I can suppress this but the image itself is not getting saved. I presume Wagtail doesn't know to handle the file saving itself - do you think there would be an appropriate place to hook in the actual storage?

Thanks! sCam

Crew sCam

unread,
Mar 9, 2016, 4:55:36 AM3/9/16
to Wagtail support
Just in case any one else is ever trying to do anything similar. I found I could manually save the image using default_storage.save in the over-ridden get_prep_value. If I had carried on down this road I guess I would have needed to find a better way of co-ercing the multipart setting on the parent form. In the end I actually implemented a view to handle the file upload as this suited the use case better. It was then just a case of adding some custom markup and JS to the widget so editors could perform the upload.

Cheer, sCam
Reply all
Reply to author
Forward
0 new messages