AnabolicHippo
AnabolicHippo

Reputation: 45

Wagtail/Django: Is it possible to populate a given admin fields options out of the results of an 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"

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
  },

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?

Upvotes: 0

Views: 662

Answers (1)

gasman
gasman

Reputation: 25292

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

Upvotes: 2

Related Questions