Unable to get formFields from callable ChoiceBlock in StreamField

41 Views Asked by At

Unable to get formFields from callable ChoiceBlock in StreamField

I was using a ChooserBlock to get Forms in a dropdown list in a StreamField on my page but now I am upgrading my wagtail version from 3.0 to 4.0.4 in which I refered that ChooserBlock is depreacted and an alternative to use ChoiceBlock was provided. While using this ChoiceBlock which gets callable choices, I am able to choose my form from the dropdown but it is not getting saved in database and as a result the fields are not exposed in my API. Adding code below for reference - Old (on version 3.0 --> working)

In StreamField ->

StreamField([("CMS_form", WagtailFormBlock())\],
max_num=1,
blank=True,
use_j`your text`son_field=True)

In wagtailstreamforms ->

class WagtailFormBlock(blocks.StructBlock):
form = FormChooserBlock(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 "), )

class FormChooserBlock(blocks.ChooserBlock): target_model = Form.objects.all()

def value_for_form(self, value):
    if isinstance(value, self.target_model):
        return value.pk
    return value

def value_from_form(self, value):
    if value == "":
        return None
    return super().value_from_form(value)

def to_python(self, value):
    if value is None:
        return value
    else:
        try:
            return self.target_model.objects.get(pk=value)
        except self.target_model.DoesNotExist:
            return None

#Post Upgrade to 4.0.4

StreamField remains same

In wagtailstreamforms -> class WagtailFormBlock(blocks.StructBlock):

This form returns empty

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 "), )

def getFormblockChoices():
return \[(form.id, form.title) for form in Form.objects.all()\]

Am I doing something wrong? Please let me know

0

There are 0 best solutions below