Skip to content

Instantly share code, notes, and snippets.

@lb-
Created September 3, 2017 12:08
Show Gist options
  • Save lb-/a3c6b8c0c3f2f0fd2574ed8cc29afd94 to your computer and use it in GitHub Desktop.
Save lb-/a3c6b8c0c3f2f0fd2574ed8cc29afd94 to your computer and use it in GitHub Desktop.
Wagtail Forms - Upload Image Field - Extend FormBuilder Class
from wagtail.wagtailforms.forms import FormBuilder
from wagtail.wagtailimages.fields import WagtailImageField
class ExtendedFormBuilder(FormBuilder):
def create_image_upload_field(self, field, options):
return WagtailImageField(**options)
FIELD_TYPES = FormBuilder.FIELD_TYPES
FIELD_TYPES.update({
'image': create_image_upload_field,
})
@ChiragKanzariya
Copy link

ChiragKanzariya commented Jul 24, 2019

How FIELD_TYPES attribute imported from FormBuilder?
Because it's shows me that FormBuilder has no attribute FIELD_TYPES @lb (Ben Johnston)

@lb-
Copy link
Author

lb- commented Aug 4, 2019

@ChiragKanzariya - the internal set up of FormBuilder has changed somewhat since this was written.
But the source code for that class can be seen here:
https://github.com/wagtail/wagtail/blob/master/wagtail/contrib/forms/forms.py#L103

Note that FIELD_TYPES is no longer a static attribute on FormBuilder but instead there is a property called formfields which could be overridden to add the 'image' type field.

I have not really had the chance to update/re-write the blog post with the new internal workings in mind, but hope that helps.

@ChiragKanzariya
Copy link

ChiragKanzariya commented Aug 12, 2019 via email

@ChiragKanzariya
Copy link

ChiragKanzariya commented Aug 12, 2019 via email

@lb-
Copy link
Author

lb- commented Aug 23, 2019

Hi @ChiragKanzariya - I don't have an updated example sorry but you can view all the Wagtail source code on their repo.

@ChiragKanzariya
Copy link

ChiragKanzariya commented Aug 23, 2019 via email

@lb-
Copy link
Author

lb- commented Aug 24, 2019

@ChiragKanzariya - awesome, if you don't mind, can you share the repo (if on github) or paste the relevant part of your code here.

This will help me and others work through this in the future, plus helps me if I get the time to write a new blog post about this.

@ChiragKanzariya
Copy link

ChiragKanzariya commented Aug 24, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment