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 响应填充.这是我目前使用的代码:
I'm working on a Django project and specifically a Wagtail project. I want to switch this code that I have below, to contain a prepopulated admin field, which will be populated by an API response. Here is the code I am currently using:
"""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 字段,并且我可以设置一个指向 MP3 文件的 URL.我想做的是从 API 响应中预先填充一个下拉菜单,如下所示:
This would enable me to create a standard Wagtail URL field and I could set up a URL to an MP3 file. What I would like to do instead is pre-populate a drop-down menu out of an API response like the following:
{
"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)我将如何编写类模型来完成这样的事情?
I'm wondering how I would go about populating the admin field with the URL from the JSON response? I guess A) this is possible and B) how would I approach writing a class model to accomplish something like this?
推荐答案
您可以通过设置 用于编辑视图的自定义表单类,并将audioUrl
的表单字段设置为ChoiceField.ChoiceField 最常与作为 choices
参数的静态选项列表一起使用,但它也允许您指定返回选项的可调用对象 - 这将是执行 API 获取的合适位置.代码看起来像这样:
You could do this by setting up a custom form class to be used on the edit view, and setting the form field for audioUrl
to be a ChoiceField. ChoiceField is most commonly used with a static list of choices as the choices
argument, but it also allows you to specify a callable that returns the choices - this would be a suitable place to perform your API fetch. The code would look something like this:
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
这篇关于Wagtail/Django:是否可以从 API 的结果中填充给定的管理字段选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!