Просмотр исходного кода

Add ability to easily use Form Page fields in API

Improve documentation. Adding information about how to retrieve form fields from the API.
Include the API fields on the form field model
Sævar Öfjörð Magnússon 2 лет назад
Родитель
Сommit
629b8e7a55
2 измененных файлов с 27 добавлено и 0 удалено
  1. 16 0
      docs/advanced_topics/api/v2/configuration.md
  2. 11 0
      wagtail/contrib/forms/models.py

+ 16 - 0
docs/advanced_topics/api/v2/configuration.md

@@ -137,6 +137,22 @@ This will make `published_date`, `body`, `feed_image` and a list of
 fields, you must select the `blog.BlogPage` type using the `?type`
 fields, you must select the `blog.BlogPage` type using the `?type`
 [parameter in the API itself](apiv2_custom_page_fields).
 [parameter in the API itself](apiv2_custom_page_fields).
 
 
+(form_page_fields_api_field)=
+
+### Adding form fields to the API
+
+If you have a FormBuilder page called `FormPage` this is an example of how you would expose the form fields to the API:
+
+```python
+from wagtail.api import APIField
+
+class FormPage(AbstractEmailForm):
+    #...
+    api_fields = [
+        APIField('form_fields'),
+    ]
+```
+
 ### Custom serializers
 ### Custom serializers
 
 
 [Serializers](https://www.django-rest-framework.org/api-guide/fields/) are used to convert the database representation of a model into
 [Serializers](https://www.django-rest-framework.org/api-guide/fields/) are used to convert the database representation of a model into

+ 11 - 0
wagtail/contrib/forms/models.py

@@ -11,6 +11,7 @@ from django.utils.translation import gettext_lazy as _
 
 
 from wagtail.admin.mail import send_mail
 from wagtail.admin.mail import send_mail
 from wagtail.admin.panels import FieldPanel
 from wagtail.admin.panels import FieldPanel
+from wagtail.api import APIField
 from wagtail.contrib.forms.utils import get_field_clean_name
 from wagtail.contrib.forms.utils import get_field_clean_name
 from wagtail.models import Orderable, Page
 from wagtail.models import Orderable, Page
 
 
@@ -121,6 +122,16 @@ class AbstractFormField(Orderable):
         FieldPanel("default_value", classname="formbuilder-default"),
         FieldPanel("default_value", classname="formbuilder-default"),
     ]
     ]
 
 
+    api_fields = [
+        APIField("clean_name"),
+        APIField("label"),
+        APIField("field_type"),
+        APIField("help_text"),
+        APIField("required"),
+        APIField("choices"),
+        APIField("default_value"),
+    ]
+
     def get_field_clean_name(self):
     def get_field_clean_name(self):
         """
         """
         Prepare an ascii safe lower_snake_case variant of the field name to use as the field key.
         Prepare an ascii safe lower_snake_case variant of the field name to use as the field key.