# Django from queryset to serialization with Iuri de Silvio

Source: https://djangotv.com/videos/djangocon-us/2022/django-from-queryset-to-serialization-with-iuri-de-silvio-djangocon-us-2022/
Date: 2022-11-23
Speakers: Speaker 1, Speaker 2, Speaker 3, Speaker 4, Speaker 5

## Summary

Django applications often turn model instances into JSON through ad hoc serializer methods, but adding related fields can quietly introduce N+1 queries, expose unnecessary data, and make query optimization the responsibility of each view. Iuri de Silvio argues that data loading and serialization should be defined together: serializers should declare the fields and relationships they need, while custom query-set and manager logic prepares that data efficiently. He presents Django Q Serializer, a library that follows this pattern, supports bulk loading of external data, and makes query counts easy to test; he also explains why DRF and automatic prefetching do not fit his preferred approach.

## Transcript

### [00:00] Model Serialization

**Speaker 1:** [00:20] Hi. First, talking about me, I'm Yuri, I'm a principal engineer at Boozer. Boozer is a Brazilian company, so we have some buses, pink buses. And I'm saying that because most of my examples are about buses. So it's easy for. And I'm from Brazil Oh how we started with yeah I will talk about my experience at Boozer and about another companies and other another projects and most of them start the same way when you have a model

[01:06] You have to convert it to a dictionary to serialize it to pass through HTTP or something else. So at the end of the day it's just a JSON. So we have to Convert it and there's no not an a simple way to do that. So most projects start with a simple method to just copy some attributes to a dictionary so your view can just call it And it works. It's a simple view. It's a JSON response.

### [01:52] N+1 Query Problems

**Speaker 1:** [01:52] I list all my buses here. They are really simple. They just have their plates So it works but when you we do that we start to add more attributes to our ser serializer this function so We start with a we start creating a problem here because now I need to add an owner to my bus. So I have to serialize that and when I do that with a foreign key, when I call the what Self

[02:37] honor it's a cu a curry and it's not explicit to a new developer, someone that don't know Django, and it still works. So now you have that. We have The same view with the same method, but now we have n plus one queries, and that's the problem that I had most of the time with Django and with most RIMs it's not only Django fault so we have that and There's not an

[03:22] easy way to fix. So I started trying things. That example is when I joined the company, Boozer. It's it was a it still is a fast-growing project. At the time we are sponsoring a soccer team We are doing TV ads and it was November so we have had Black Friday there with crazy promotions and our pro the project was already big so it was a we each model had a serialized method like that Not like that with a lot of relationships. So

### [04:08] Queryset Optimization

**Speaker 1:** [04:08] our servers and our database were always overloaded, and my main job there was to start improving their code base and that's the thing I'm doing since the beginning three years ago and it's an endless job and I really like that the subject. So how to fix it? Here we had here we add a did owner And now I have to make sure that I have only one query or something better than any plus one curious

[04:53] queries because when I have Just some objects you don't even know it's a problem, but when you scale it's a really bad problem. And or with a huge volume of requests you will understand how i it works. So It's easy to fix. You can just change our key reset to add a selector related and that way Django will do join at the x sql level and now I can have only one carry again But it's not good

[05:38] because my query set is in my view and my serialization is defined somewhere else in the model so it's not easy to handle it and when it's just a an example but record base heads hundreds of views probably at that time and now a lot more so it's not easy to scale that code device so Django has Houston Care sets where I can encapsulate this issue and create my Trans transform that in a two-step problem. First I have to say what I want to serialize

[06:25] What I want to load, that's the owner, and after that I have to say that I want to serialize. So now I It's in my model. So here is the custom care set. And here is a I'm overriding the default model manager. So now I in my view I can just call it to serialize and after that I can serialize and I have only one query again It works, it's better than the other solutions, but we with the solution We have only one serializer

### [07:12] Context-Dependent Serializers

**Speaker 1:** [07:12] for all my model and each view needs something different, needs More attributes or less attributes. In some cases, if I pass everything, I can expose sensitive data. So It's it's still not good, it's better than before. And because I have only one way to serialize, I will put there everything I need. So if I have 20 relationships, I will put everything there. But I need I probably never need all the 20. I need one in one view, another in other view, and if I

[07:59] I don't have the option to reduce the problem I just put everything there So, what we're we are seeing there, each view has different requirements And my serialization must know what it needs to load. And that's all context-dependent. So I have three steps. The first one I'm loading the data. I'm doing some business logic things here, and after that I serialize my data to send back to the clients. And we when we are cu creating our query sets, we are thinking about the first step.

[08:47] We load the data we need to Do our business logic, but we forget the next part that serial the serialization needs to know The serialization needs data that were loaded before, or it will go to any plus one problems or to bad ways to load this data and it's not good. The and that that's the reason I I'm trying to

### [09:32] Django Q Serializer

**Speaker 1:** [09:32] do that. I'm trying to link loaded data with serialized data And I because of that I created the a library. It's Django Q serializer. You can it's open source, you can install and check the GitHub that and how it works. It's I just first override my manager with a custom manager that's my library will handle And after that I have to define serializers that know what they need to load.

[10:18] So in this case I know I'm defining the serialize object function, that's the base function for the serializer. That needs the plates and that needs the owner. So I can say for my serializer that you have to load select related and following the past examples you can Just extend how it worked and instead of calling to serialize with nothing, now you can call it with your custom serializer And that way the curie will join with the company to

[11:06] load the owner and when you use the realizer you have only one query that way we if we almost fix the problem if I need each view needs a new a different serializer and I can just create many serializers. Of course you need to create it thinking about not If you create many serializers, it's difficult to maintain. So it it's a bit catchy, but it's better than before. So here I have some examples

### [11:51] Advanced Serialization Patterns

**Speaker 1:** [11:51] and more complicated than the base one. Here I I'm changing my key reset so I can use everything Django already has and just return I just overhide the prepare carry set and return with everything I need and when I I'm going to serialize I just use the defined state. I'm annotating the objects here. So I need that on in this case so it's all encapsulated and I can

[12:36] I don't need to watch to see another file and another view and I know everything about the serialization based on that Another example when I need external data, it's a common use case we have. We In this case I want to get my location of the buses. So I have a bus location service that doesn't matter what it does, but what matters is When I can override the process objects

[13:21] that receives all the objects in the this case are buses. So I get the geolocation for all my buses in an optimized way, and after that I just attach it to my object so now when I will serialize that I have access to my bus geolocation and I can do that and I know how I access the data before and how to attach it when I have to serialize. After I built that I learned that it's really good to

### [14:08] Query Count Testing

**Speaker 1:** [14:08] Test how much many carries I'm doing and doing that I guarantee that someone no never will add another carry by accident and that's common. So Now I can my it's just a simple Django test. I'm I'm creating my bus here, my object. And s with Baker, so I just tell him to fill all optional fields and relationships so probably I I'm covering most cases here just doing that and I can Test my serializer

[14:54] here just asserting that that we 'll do only one query, the only query that it does is loading the buzzes inside here the decorator the context manager and I know it it's I know it has only one query and it's very good because I can do that and I can do that easily for almost all my serializers and my models in an easy way maybe it can it can stay in the inside the library because it's really useful test to do

### [15:39] Serialization Patterns in Django

**Speaker 1:** [15:39] This lib is I like it to create this lib because it's easier to learn when you start using it. It's a bit complicated than change your queue set but when we you have to do that you are forced in the right path you will always see what you have to load and what you can consume later and it's easier to code review too because everything is together and it's easier to test because you can count and test only the serializer not thinking about other parts of your system. So

[16:24] Keep counting is painful without this isolation. But it's not about this leap, my talk, it's more about the pattern that we found in a lot of projects. So that happens because Django don't have an easy way and an obvious way like Zen of Python 6 and at Django Kong we had we already had a lot of talks talking about workarounds of R or R because it's a common issue. We n need talks about

[17:10] select related, perfet related, how to handle that with jung DRF. That have serializers and other things and we have other people working things near what I did. Most of these projects are new, so when I started I already knew DRF but I don't use that and it has some issues. I already knew Django Auto Prefetch, but it doesn't solve the problem, it just hides it. You can't when you install AutoPrefetch. It will when you execute a query set, it will

[17:56] automatically load all related objects with the same pattern. you don't know how it it's doing the their thing and it the problem here is you are not making the developer think about the problem so You're just hiding it in a better way than before, but you're still hiding it. And here I linked some projects. Django Readers, Django Strawberry Plus that had Tutorial yesterday, junk virtual models that Flavio will talk tomorrow, and probably he will talk more about all of them.

[18:41] So It's I issue that I want to fix or improve how we do that with Django because it's really painful to me and that's because I Talking about it. Thanks. That's it.

### [19:08] Questions

**Speaker 2:** [19:08] Thank you very much, Yuri. We have time for a few questions. So if you've got a question, raise your hand. Yeah.

**Speaker 3:** [19:15] Thank you. Great talk. So I have a question. So I'm using because I can see in my scenario where I can fit this in my project is not having those query sets in the views and then I can move it easily to the serializer level I'm just wondering, does it affect performance? Have you compared the performance between the traditional way and then using this package? Is it gonna be decreased? Is it gonna be the same or is it gonna be more?

**Speaker 1:** [19:44] I don't think it in it impacts performance. The serializer is really simple and it's it just calls some functions. You can see the implementation at the repository but the implementation is really simple it doesn't don't have too much met too many magic too much magic it's just I just organize the code

**Speaker 4:** [20:16] I know this is an impossible question to answer, but What when would you say like because I feel like a lot of projects it depends on the scale of the traffic or the performance and like you know how do you know when to start thinking about this as a problem that you would or is it part of your pitch is just use the library from the get-go and you'll you'll never even have known. You know, like for when you're trying to quickly start with a project or on board, you know, how do you know when you've like crossed that level where it's important to Optimize.

**Speaker 1:** [20:52] Yeah, I understand and I understand the question. When you the project is small, you don't need to think about that. It will work for a long time and it just works. But When you start thinking about that soon you have the it's easier to scale because I I'm saying about Q serializer but still today three years later I started at the company we have a lot of serialized methods and it's impossible to remove them because it's a lot of legacy code. So if you start soon it will be easier to maintain the project.

[21:40] But I always started thinking about that when it's already crashing. All my projects were this way. So I don't recommend that. We

**Speaker 2:** [21:57] got another question. If there isn't, I'll slip one quick one in. You mentioned at the end the Django Rest framework and you said it had problems. Django Rest framework always would have been my sort of go-to for serialization. I'm intrigued what your what problems you were hit you were you were thinking against.

**Speaker 1:** [22:11] Problem is you need to use DRF to use then. So it doesn't work does doesn't work for me. And I don't like it's related to a view set, so it's still far from the serializer is far from the Q set so it's difficult to see when you change somewhere you have to change your QR set and It has this way the same problems at the end of the day. Sure.

**Speaker 2:** [22:44] Any other questions? We have to do that.

**Speaker 5:** [22:53] Thank you Yuri for the talk. Have you considered combining your serialization object method with some kind of tool like Bydantic? to format the data. For example, if you return a date time there, I guess you should format first to string, right? Yeah. So have you used those tools like Pydenic? I think it would be a great fit there

**Speaker 1:** [23:18] Not yet, but it's a good idea. So maybe. He is Flavio that we'll talk tomorrow.

**Speaker 2:** [23:35] Okay, if there are no other questions, he thank you very much, Yuri, for your contribution to the conference and a small token of appreciation from the organisers.
