Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...