Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

django - How to set custom choices in Wagtail form?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...