New Wagtail Course! 🥳 The Ultimate Wagtail Developers Course

Tutorial Wagtail Version: 2.x

Headless CMS: Exposing Custom Page Fields to the v2 API

In the last lesson we enabled Wagtails v2 API but we didn't have access to our custom fields.. yet! In this lesson we'll learn how in just 4 lines of code we can make a custom Wagtail Page field show up in our API. And it only gets easier from there.

In the last lesson we enabled the Wagtail v2 API by writing 12 line of code. But our custom fields didn't show up. But to make these custom fields show up as JSON output in the Wagtail v2 API is really easy. In fact, it's just 4 lines of code. And for every extra field you want to expose is just one more line of code. Let's take a look at exposing a custom field in a Wagtail Page.

``` from django.db import models from wagtail.core.models import Page from wagtail.api import APIField class HomePage(Page): """Home page model.""" banner_image = models.ForeignKey( "wagtailimages.Image", null=True, blank=False, on_delete=models.SET_NULL, related_name="+", ) api_fields = [ APIField("banner_image"), ] ```

In the above example, we're using a ForeignKey to a Wagtail Image, and near the bottom we're simply exposing that field to the v2 API. And if you opened this page in your v2 api viewer (ie localhost:8000/api/v2/api/find/?html_path=/) you should see the banner_image with a bunch of extra data, like a download_url, detail_url, and even the image id.

And that's it! If you want to expose more fields, simply add more APIField()'s to your api_fields model property.

Related tutorials

Headless CMS: Fetching Data From the v2 API

Posted on

The Wagtail v2 API is how we can turn our website into a Headless CMS. By default Wagtail will give you specific fields in the form a JSON object. But there are times when you only want certain fields; you might want to exclude or include specific fields in the JSON response. In this tutorial, we'll explore exactly how to do that.

View lesson, Headless CMS: Fetching Data From the v2 API

How to Enable the v2 API to Create a Headless CMS

Posted on

Wagtail comes with a lot of really powerful features. Many features are not enabled by default as to keep your site running quickly and efficiently. One of those amazing features is the Wagtail v2 API, which can return any page, image, document, orderable and StreamField as a JSON response for your SPA/PWA to consume. We'll enable this by adding just 12 lines of code to our Wagtail CMS website.

View lesson, How to Enable the v2 API to Create a Headless CMS

Headless CMS: Custom Page Properties

Posted on

If you want to add data custom data (or a function output) to your headless Wagtail API, you can use a @property. In this tutorial I'll show you how easy it can be!

View lesson, Headless CMS: Custom Page Properties

Headless CMS: Exposing Orderable Data and StreamFields

Posted on

In this tutorial you will learn how to add Orderable model fields to your Wagtail v2 API, and how to add StreamFields to your API response. As with everything in Wagtail, this is a simple task for developers.

View lesson, Headless CMS: Exposing Orderable Data and StreamFields

The Ultimate Wagtail Developers Course

This course covers everything from basic installation to advanced features like custom blocks and API integration, it's perfect for developers looking to enhance their skills with this powerful CMS.

Ultimate Wagtail Developers Course Logo