Tutorial Wagtail Version: 2.x
Using Django Rest Framework with Wagtails v2 API, we can customize ANY field the way we want. In this short video, we take a look at creating a new field entirely by overwriting an image, but we also look at serializing (JSONifying) an image field from a ForeignKey inside an Orderable. Don't worry, it's not as crazy as it sounds!
In the previous lesson we ran into an issue where a ForeignKey image was not serializable. In this video we're going to address that problem by creating a custom serializer using Django Rest Framework and Wagtails v2 API.
In true Wagtail fashion, we don't need to do very much work. I know I've said this about 10,000 times by now, but this is one of the biggest values behind Wagtail CMS: You can achieve almost anything in such a short amount of time, and still have efficient, maintainable and elegant code.
Let's dive in!
A serializer really just takes some information from Django (or in our case Wagtail) and turns it into a JSON response for the browser and frontend frameworks like React to consume.
This takes the image as a value. You'll see where it's getting the image (as the value) in the code below. But if you wanted to extend this to any other field type, you can absolutely do that too.
Interested in seeing the full git commit? Well don't be too shocked when you don't see very many changes ;) Wagtail makes the lives of developers super simple.
Headless CMS: Fetching Data From the v2 API
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.
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.
How to Enable the v2 API to Create a Headless CMS
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.