Correct way to add dynamic form fields to WagtailModelAdminForm #9870
andrewebdev
started this conversation in
General
Replies: 1 comment
-
I ended up creating a separate A custom Panel then looks for this new form instance as an attribute of the primary form. As an instance of the primary form, I can "clean" this internal form when the primary form I then customized the It's a bit of a workaround, but works well enough for now. I wish there was an easier way, but it doesn't look like it right now. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a use case where I need to add dynamic form fields to a
WagtailModelAdminForm
. With standard django I would normally just create a custom subclass and add the fields in the__init__
method of the form. In Wagtail, because the forms are built up with the edit_handlers, this becomes a nightmare to deal with.I have the following dynamic form:
Next I customized the ModelAdmin
EditView
(attributes are not present in the create view):Here is the
get_product_attributes_panel()
function for reference:So the problem is...
A) Adding the ProductForm in the way I did above (by using it as the base_form_class in TabbedInterface) works, in that it does add the fields to the form; BUT I have no control over the rendering.
B) If I uncomment the line
ObjectList(attributes_panel, heading='Attributes'),
(to get nice rendering of the fields), then I get an error for my dynamic fields, saying that they are missing.This is a very important requirement in the project I'm working on.
A temporary workaround is to create a custom panel to render the dynamic fields directly in the panel template; But then I lose the Django Form validation, which is also an important requirement for this.
Is there any way to add dynamic fields the the WagtailModelAdminForm, that preserves the modeladmin features such as formsets, permissions etc.
Note: I also cross-posted this to https://stackoverflow.com/questions/75070527/correct-way-to-add-dynamic-form-fields-to-wagtailmodeladminform
Beta Was this translation helpful? Give feedback.
All reactions