Back to the video

How to abuse Wagtail's StreamFields as much as you want

Transcript

By Rémy Sanchez. From Wagtail Space NL 2024. Published June 27, 2024.

Watch the video Markdown version 1,098 words · auto-generated · en

Summary

Wagtail StreamFields can make migrations enormous because Django serializes the entire nested field definition when even a small block option changes. Rémy Sanchez showed a custom migration-packing script that extracts large literals into separate files and replaces them with lazy objects using `lazy-object-proxy`, reducing a generated migration from 5.1 MB to 280 KB and avoiding high memory use when loading migrations. The solution is hard-coded and hacky, but addressed migration size and memory problems at their scale.

Transcript

Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.

0:00 StreamField Block Growth

Ooh, funky. All right. So I'm going to do the risky thing and do a live demo there. The concept of the talk is how to abuse the stream field as much as you want, given that I'm not going to represent the same thing, but it's the same as we've seen this morning. You know, a lot of us I think are using design systems that translate into blocks that go into stream fields and that allow to have huge flexibility and that's an amazing feature of Wagtail that really you know allows us to do anything we want. But the only drawback there is that if you are not careful with what you do

or for whichever reason you need to adapt different stuff and so on You quickly end up with many different blocks that reuse or like different kinds of pages and so on. So for example, here is my file full of blocks You see this thousands of lines long and there's lots of definitions of lots of kinds of blocks etc. And here for example I have like standard button colors okay that that are in my UX. So suppose my designer says now I want to have a blue button okay So I say blue here. So I just added one option, right? So I'm going to make a migration because otherwise Django is

1:41 Generating Oversized Migrations

going to complain. So I'll make tick Two to two all right, no, not this. There we go. I'll make the migrations. So You might be wondering why it's it has not returned yet and I will ask in between a question to the audience what do you think is going to be the size of the migration file so when I ran it earlier it took forty seconds to generate So let's wait a little bit. I can do a dance in between. But basically since you know it's nested inside many pages, many stream fields and so on, like all those fields individually have been affected

And you don't change just this property inside this field, you change the whole field because it's the granularity of Django. So it does not go inside the field. So Let's see now what happened there. So you see that this thing has been you know appearing and it's 5. 1 megabytes. So that's completely insane and if you look at that well you see there's many lines, yes. So Pretty impractical in the end. And we initially we did not even realize this, but it's just we were running the migrations on the Docker

3:16 Migration Memory Costs

container that has like two gigabytes of RAM or I don't know three And it was running out of memory and we were like, what the fuck? You know, when you start the when you run server, it's for hundred megabytes of memory at start or whatever, but then it was exploding. So Basically just loading the migration literals into RAM was exploding the thing and the migration folder was 500 megabytes. It was completely insane. So I had to do something about it that is well don't call the exorcist please that is basically I created Like I created a little command here that is migration packing that is basically going through the migrations and looking for those huge literals.

4:03 Packing Migrations with Lazy Loading

That are very big and removes them from the file and transforms them into lazy loaded objects. So you'll see in a minute what I mean. is that here all the stream fields got separated into different little files that are containing the same thing okay but it's just all condensed, there's no you know indentation or anything. It's just all the same. So if you look at the size of the thing like we went from five megabytes to 280 kilobytes for the whole migration, which is already a huge improvement. Like your Git is going to be happy about it

And on top of that it's less things to load, but on top of that, it's also you don't have to load it if you don't need it because now what migration looks like is this. It is just having a lazy stream block here in the parameter of the stream field. And that's it. And I am giving here the name of the heavy literal and I'm using what's the name of the module? Proxy lazy object. So basically it's a Python module that makes a lazy object. So it will it's indistinguishable from your real object. From any other part of the system, but it will just load the object when you access

try to access a property or do something with it. And in that regard it allows you to load all the migrations without loading the literals of all the stream fields and without overrunning the RAM. And so We went from using gigabytes of RAM to random migrations to a lot less, and we also decreased a lot the size of the migrations folder. So that's little bit hacky of course because there's so unfortunately it's not open source because it's really disgusting what I did there, but it works you can have a look. I'll show you. Must be somewhere. Tick No, not this one.

6:20 How the Packing Script Works

Well back and about to put it here. So basically what I do is that I have this whole script that is You'll see running through the like it's really hard-coded not nicely. That is running through the AET of Python and that is just dumping the AST into different files. And then I have this static bit of code that is you know generating the lazy stream block based on the stuff and so it's lazy object proxy that I'm using. So yeah, basically that's it. But I must say it has been very important for us to have this because at this scale of dementia on migration size sizes we needed something like this. I

don't know if you have questions.