I’m trying to add a conditional field to a wagtail page type model, the conditional fields may look like as shown in the below image. There are two fields, Question and Answer. The Answer field values should be displayed with respect to the selection of the Question field
If we select Toyota from Question then Camry and Land Cruiser should be displayed in the Answer drop down, if we select Honda then Civic and Accord should be displayed in the Answer.
In blocks.py I have two classes which are responsible for Questions and Answers fields respectively
class ConditionsQuestionBlock(blocks.ChooserBlock):
widget = Select
class Meta:
icon = "question"
@cached_property
def target_model(self):
return Question
@cached_property
def field(self):
return forms.ModelChoiceField(
queryset=Question.objects.all(),
widget=self.widget,
required=self._required,
)
def value_for_form(self, value):
if isinstance(value, User):
return value.pk
else:
print("selected q:",value)
selectedqval=value
print("selected qvalue:",selectedqval)
return value
class ConditionsAnswerBlock(blocks.ChooserBlock):
widget = Select
class Meta:
icon = "question"
@cached_property
def target_model(self):
return Choice
@cached_property
def field(self):
choice=Choice.objects.all()
return forms.ModelChoiceField(
queryset=choice,
widget=self.widget,
required=self._required,
)
def value_for_form(self, value):
if isinstance(value, User):
return value.pk
else:
return value
Now irrespective of the Question selection I'm getting all the Answer options