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

Unable to export form field through API fields #217

Open
shinderucha opened this issue May 16, 2023 · 1 comment
Open

Unable to export form field through API fields #217

shinderucha opened this issue May 16, 2023 · 1 comment

Comments

@shinderucha
Copy link

Hi All,

I am trying to choose a created form through WagtailFormBlock() using ChoiceBlock and then export the selected form through API fields.
Code for ChoiceBlock is as below -

def getFormblockChoices():
choices = []
forms = Form.objects.all()
for form in forms:
choices.append((form.id, form.title))
return choices

class WagtailFormBlock(blocks.StructBlock):
# FORMBLOCK_CHOICES = getFormblockChoices()
form = blocks.ChoiceBlock(choices=getFormblockChoices, required=False)
form_action = blocks.CharBlock(
required=False,
help_text=(
'The form post action. "" or "." for the current page or a url'))
form_reference = InfoBlock(
required=False,
help_text=
("This form will be given a unique reference once saved"), )

However, I am able to choose a form and publish the page but the fields are not visible through API.

Please let me know.

@kirillwolkow
Copy link

You can override the get_api_representation() method to get your fields. Here an example:

class StreamFormBlock(blocks.StructBlock):
form = WagtailFormBlock()

def get_api_representation(self, value, context=None):
    value_dict = dict(value)
    form = model_to_dict(value_dict['form']['form'])
    fields = form['fields']

    return {
        'id': form['id'],
        'title': form['title'],
        'slug': form['slug'],
        'submit_button_text': form['submit_button_text'],
        'success_message': form['success_message'],
        'error_message': form['error_message'],
        'process_form_submission_hooks': ['save_form_submission_data'],
        'form_action': f"/api/v2/streamforms/{form['id']}/",
        'form_reference': value_dict['form']['form_reference'],
        'fields': fields._raw_data,
    }

class Meta:
    icon = "form"
    label = _("Stream Form")

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

No branches or pull requests

2 participants