I am fetching a list of items from another project, and want to show that list in my form. I tried overriding ModelChoiceField
to define choices, but it is not working:
from django.forms import ModelChoiceField
class CustomChoiceField(ModelChoiceField):
def _get_choices(self):
# Overriding ModelChoiceField's _get_choices() method to set custom choices
self._choices = my_utils.get_choices()
return self._choices
from wagtail.admin.forms import WagtailAdminPageForm
from wagtail.snippets.widgets import AdminSnippetChooser
class MyCustomForm(WagtailAdminPageForm):
my_field = CustomChoiceField(queryset=SomeModel.objects.all(), widget=AdminSnippetChooser(SomeModel), required=False)
I want to choose from the list that was returned by the API call, and whatever the user chooses, should be saved in the my_field
.
How can I achieve this?
question from:
https://stackoverflow.com/questions/65937164/how-to-set-custom-choices-in-wagtail-form 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…