Wagtail/Django:是否可以从 API 的结果中填充给定的管理字段选项?

Wagtail/Django: Is it possible to populate a given admin fields options out of the results of an API?

我正在做一个 Django 项目,特别是一个 Wagtail 项目。我想切换下面的代码,以包含一个预填充的管理字段,该字段将由 API 响应填充。这是我目前使用的代码:

"""Flexible page."""
from django.db import models

from wagtail.admin.edit_handlers import FieldPanel
from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.images.edit_handlers import ImageChooserPanel

class FlexPage(Page):
    """Flexibile page class."""

    template = "flex/flex_page.html"

    # @todo add streamfields 
    # content = StreamField()

    subtitle = models.CharField(max_length=100, null=True, blank=True)
    Flexbody = RichTextField(blank=True)
    bannerImage = models.ForeignKey(
        "wagtailimages.Image",
        null=True, 
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+"
    )
    audioUrl = models.URLField()
    content_panels = Page.content_panels + [
        FieldPanel("subtitle"),
        FieldPanel('Flexbody', classname="full"),
        ImageChooserPanel('bannerImage'),
        FieldPanel('audioUrl'),
    ]

    class Meta:  # noqa 
        verbose_name = "Flex Page"
        verbose_name_plural = "Flex Pages"

这将使我能够创建标准的 Wagtail URL 字段,并且我可以将 URL 设置为 MP3 文件。我想做的是从 API 响应中预填充一个下拉菜单,如下所示:

   {
     "id":"83ee98f6-3207-4130-9508-8f4d15ed7d5c",
     "title":"some random description",
     "description":"some random description.",
     "audio":"https://somerandomurl.mp3",
     "slug":"some random description",
     "draft":false
  },
   {
     "id":"83ee98f6-3207-4130-9508-8f4d15ed7d5c2",
     "title":"some random description2",
     "description":"some random description2.",
     "audio":"https://somerandomurl2.mp3",
     "slug":"some random description2",
     "draft":false2
  },

我想知道如何使用 JSON 响应中的 URL 填充管理字段?我想 A) 这是可能的,B) 我将如何编写一个 class 模型来完成这样的事情?

您可以通过设置 a custom form class to be used on the edit view, and setting the form field for audioUrl to be a ChoiceField 来完成此操作。 ChoiceField 最常与静态选项列表一起用作 choices 参数,但它也允许您指定可调用的 returns 选择 - 这将是执行 [=16] 的合适位置=] 获取。代码看起来像这样:

from django import forms
from wagtail.admin.forms import WagtailAdminPageForm

def get_mp3_choices():
    # API response is hard-coded here, but you would fetch and parse the JSON instead
    files = [
        {
            "id":"83ee98f6-3207-4130-9508-8f4d15ed7d5c",
            "title":"some random description",
            "description":"some random description.",
            "audio":"https://somerandomurl.mp3",
            "slug":"some random description",
            "draft":False
        },
        {
            "id":"83ee98f6-3207-4130-9508-8f4d15ed7d5c2",
            "title":"some random description2",
            "description":"some random description2.",
            "audio":"https://somerandomurl2.mp3",
            "slug":"some random description2",
            "draft":False
        },
    ]

    # return a list of tuples where the first item is the value to store,
    # and the second item is the human-readable label
    return [
        (file['audio'], file['title'])
        for file in files
    ]

class FlexPageForm(WagtailAdminPageForm):
    audioUrl = forms.ChoiceField(choices=get_mp3_choices)

class FlexPage(Page):
    # ...
    base_form_class = FlexPageForm