Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Form with pre-defined set of fields #156

Open
lingster opened this issue Sep 5, 2020 · 4 comments
Open

Create Form with pre-defined set of fields #156

lingster opened this issue Sep 5, 2020 · 4 comments
Labels

Comments

@lingster
Copy link
Contributor

lingster commented Sep 5, 2020

Hi,
Is it possible to create a form which is programmatically pre-defined with a set of fields. I'd like to ensure that my forms always has at least 1 input text box, but I'm stuggling to see how to add this.

@ababic
Copy link

ababic commented Sep 6, 2020

@lingster you could do this by overriding the base_form_class on the Form model, and setting an initial value for fields in the __init__ method, but it would be nicer to be able to swap out the form model completely, then you could simply override __init__ on your custom model.

@nnist nnist added the question label Sep 6, 2020
@lingster
Copy link
Contributor Author

lingster commented Sep 7, 2020

@ababic : indeed this is roughly what I have tried:

class MyPageForm(WagtailAdminModelForm):
    def clean(self):
       # my custom code 

class MyForm(AbstractForm):
    base_form_class = MyPageForm
    def __init__(self):
      ....
    
@modeladmin_register
class MyFormModelAdmin(FormModelAdmin):
    model = MyForm

The trouble with this method is that we will now have additional table created to store MyForm. I'm just looking for a cleaner way to implement this, could you do something like Form.base_form_class = MyPageForm instead?

@ababic
Copy link

ababic commented Sep 7, 2020

could you do something like Form.base_form_class = MyPageForm instead?

This is the approach I have taken in a recent project.

@lingster
Copy link
Contributor Author

lingster commented Sep 7, 2020

Yes, that does indeed do the trick. I can add some documentation for this, if it will be useful for future users?
Perhaps a new forms.rst ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants