sqwale
sqwale

Reputation: 575

Django Wagtail forms based on models

I have a simple model that references my Auth_User_model. I want to create a simple form that creates an OAuth2Client which inherits from functionality from OAuth2ClientMixin.

class OAuth2Client(models.Model, OAuth2ClientMixin):
   user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)


class FormPage(AbstractForm):
    class Meta:
        model = OAuth2Client

What I am unclear about is how to bind my Form Page to my model? I'm using this high level tutorial but seems a bit geared to the AbstractEmailForm. Which doesn't work for me. How do I create a form based on a model?

Upvotes: 0

Views: 630

Answers (1)

gasman
gasman

Reputation: 25292

The wagtail.contrib.forms app is geared towards simple data collection such as surveys, where the form fields are defined editorially through the admin interface - this isn't the case here, where the form fields are inherently tied to the model definition set up in code. A Django ModelForm is a better fit:

from django.forms import ModelForm

class OAuth2ClientForm(ModelForm):
    class Meta:
        model = OAuth2Client

If you want to incorporate this form into a Wagtail page rather than a plain Django view, you can override the serve method on the page model, which has very similar behaviour to a Django view function (taking a request object as an argument, and returning a response object) - this is where you would put the logic for checking request.POST and processing or rendering the form accordingly.

Upvotes: 1

Related Questions