Lightning Talks Day 3
Transcript
By Speaker 1, Speaker 2, Speaker 3, Speaker 4, Speaker 5, Speaker 6, Speaker 7, Speaker 8. From DjangoCon Europe 2022. Published October 17, 2022.
Watch the video Markdown version 4,607 words · auto-generated · en
Summary
The lightning talks present practical Django and Python techniques: wrapping Django’s test client to enforce database query limits automatically, extending Django template tags to accept a rendered body, and selecting apparently random records efficiently with a stored random value. Other speakers show how GNU Make can manage Python and Django workflows, promote Django Girls workshops and their inclusive learning materials, use Django Admin with a REST API as its data source, and automate Django upgrades with the Django Upgrade tool. Together, the talks argue for small, pragmatic tools that catch performance problems early, simplify developer workflows, broaden participation, and reduce repetitive maintenance when evolving Django projects.
Transcript
Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.
0:00 1st Talk
Speaker 1
Okay, so hey guys. Just have to
Speaker 2
Wait.
Speaker 1
Okay. Okay, so introduction speech, right? So just wh while I was coming here to here to the stage, I thought that you know Coming here after the closing ceremony speech is like coming to a bar after it's working hours just to get some beer. But hopefully but hopefully it will went fine. So yeah, good day guys. Sally I have only five minutes, but I will try to make it as quick as possible and as detailed as possible So database query counts checks and tests. Definitely it's not some kind of new, I don't know, reinvent or something like that, but let's try just hear it out. Firstly, very shortly about me. I'm Justin. I'm a software developer and team lead of 15 great Django and Python developers back in Lithuanian corn case technologies.
We're development agency. I'm Python and EWS enthusiast and yeah, I'm first time standing here in the DjangoCon. Even if it's for five minutes, probably it still counts, right? Great. Motivation. We all know how to optimize queries, right? There were tons of really great topics about testing Optimization and so on and so forth for select related, perfection related course and other cool things. But the problem is sometimes it's very hard to spot where to optimize it, right? And how I spotted that when doing audits, when joining, when augmenting other teams, joining other projects on the Python and Jingo, just like
very simple example right here we've got like you know in serializers we're just trying to get some foreign key relationship, but we forget to perfetch it, to select related interviews. And immediately we get additional queries to the database and well you know what happens when we got some list endpoint with 20 or more items. So what are the possible solutions? First possible solution you can let your application be tested by the clients, by the users. Yeah, react fast, try to optimize everything That's totally a solution, right? This can be counted. We can write tons of great integration load end to end tests, but They are pretty hot to maintain to write and eventually you spot that
you got the entire team taking care of that thing. Or third thing, just maybe try it in unitests. Try to catch at least a little part of those of the performance issues in unitests. How to do that firstly you can already try checking that in your unit test without any hard thing. For example, in case you've got PyTest, it's Django Assertnum queries fixture. In case it's unitest library, you've got self dot num queries But it's not very convenient because you need to still use context managers. What is the solution which we're using in our company? It really paid us I would say well, is make a wrapper around the API client to assert the B query accounts implicitly.
So this means without doing any additional thing so that your query accounts would be checked under the hood. There will be some code so I'll just try to make very quickly through them. So what we did yeah, basically we just wrapped API client. This can be REST frameworks, this can be Django 's. It's actually not a big difference. What we said we just allowed to pass default query limit And later on we just overrides all the possible and all the possible calls, all the possible methods post, get, and so on and so forth But basically we just wrap them in context manager so that we would do this implicit checking of queries. And the helper the helper
the helper just returns that context manager 's context And in the next slide, what that assert maximum queries context is. Basically, there isn't much magic. Django already helps us to do that within the utilities because it provides us capture queries context what it does underhood under the hood basically just captures all the queries within your tests, right? All the queries to the database. So here if we don't need to write anything for that. Then we pass maximum query limit, which is which is sufficient. And what we do, we just go through captured queries. There is some com very small conditional just to get rid of transactional begin and commit lines.
And what we do we just Get the count of them, right? Very simply the length function. And we check whether it's less or equal to the maximum query limit which was passed. Right? And lastly, if not we just raise an error. So basically in PyTest we just raise value error. In unit test library we can just do self dot fail so how do tests look now? We can provide query limits when making a call. We can also provide the default value so that you know you can just override your fixture, for example, for the client And all your tests already will be doing this check implicitly. So without you know any more test updates. And
in case you really want to change that for your existing test, you can just pass query limit one. So where is that coming from?
Speaker 2
Yeah.
Speaker 1
Yeah so this
Speaker 2
god.
Speaker 1
Yeah, so they are some to do, so in case you're interested, just give me a ping. We might release the library at some point So thank you for listening. Here is my LinkedIn. Let's connect. Thank you for listening.
Speaker 2
Thank you. Thank you so and
5:51 2nd Talk
Speaker 3
me again. I'm Seb. I won't be presenting myself, it's too much time, and I have a boat to catch So my lightning talks about sleepers. You may have heard that on Wednesday by Thibaut Colas, and I'll justify wha why I choose this title. So I'm here to talk about Django templates, sorry. And you can do basic things with Django templates. Like it's very simple you register oh sorry Doesn't work? Yeah. Better? Yeah. Okay.
Speaker 2
I pause it.
Speaker 3
So inclusion tag. Basically you f you pass a template name and some variables and yeah you just you can use it as a jump link. For example. But what about if you try to put some HTML in that on the on the bottom? There's no simple way to inject HTML in answer variable in this example. So What if you could do that? Like let's say I have a an FAQ tag which I gave which I give some parameters and then I have a body that can be used in the template that that I include.
So To do that you can use sleepers, which is a great library. So basically you declare a template. And then you declare it in the YAML file. Well, okay. And then you put it in your template why by loading slippers and use it But I don't like YAML, so I have thought about doing my own and let's hack inside the Django template code So let's let's try to do a custom template node. You know the decorator register inclusion tag re relies on inclusion node And it just can't handle parameters, so we just need to handle parameters and a body.
So we are going to declare our own. Let's say we have an inclusion body with body note. And we'll take a node list, which is a body of our template tag, and a context object name, which will be answer in our in our case. So yeah, node list and context object name. Now we need another registration decorator. So let's call it register inclusion with body. And it has the classic parameters to inclusion tag. So that may be complex for you, but basically I take the normal parameters from a register inclusion tag And then I just add context subject name. Now
the goal is to use that decorator like that. So a register a function and then I name in I name it FAQ, I give it a question parameter, an active keyword parameter, I do some things inside of that. like putting an FAQ counter which will be useful. And yeah so let's see how do we get from how where can we go from that So we will need to know which parameters are given to the FAQ decorated question. For this we use getFull Arg spec. This takes the name of the decorated function and
oh yeah this takes the name of the decorated function in case in case we didn't give it a name We have a parsing compilation business, I'll get back on that later. And this function is finally registered in the template library. So again, the goal is to do that So we have the parameters, we have the body, the node list, and we parse it until the end tag Now about the parsing part. You take basically all the bits without the FAQ tag name And then this code analyzes the parameters after the FAQ tag name and maps them to the one we have in the decorated
function. So Yeah. That passes the node list until the end tag and it excludes the first token Which is the starting tag and finally we give all of that to our new node and yeah believe it or not that's it we you just declared a new kind of Django template node You declare a new decorateur and instant shade. Another use case you can do is some kind of panel with a middle tag, so you can have two bodies and yeah
Speaker 2
Thank you.
11:19 3rd Talk
Speaker 4
It's the first time on any conference stage for me, so I'm a little bit nervous. I work at Ambient, which is luckily here on the sponsor list as well, which is a really cool thing. And I had the other day a problem with which I thought couldn't be so hard to tackle and was picking a random or at least more or less random record from a table. And there's this wonderful order by question mark thing. Here in documentation, which works, has this annotation that it might be expensive and slow, which is true, at least in our setup, with a couple of thousand records. I got a friendly email from my ops guy telling me that What am I doing there? So I had to find another solution.
And then I Googled it because I thought I can't be the first person, it can't be that hard, right? And there's like Lots of suggestions which are all not so which might not be very performant depending on the distribution of your data. So I was like thinking okay there must be a really easy solution and in the end I came up with just adding a new column to my table and adding something semi-random like UUID or hashing some values together And just adding a new column and pre-filling this in with the great Django pre-save signal. And Yeah, just ordering then by this new column and this is I mean it's not really random, right? But it feels random and it was completely sufficient for my case
And unless I don't know you have like huge data with millions of records or something and then you will create lots of extra data. I think this is a very easy solution to you know get something very efficiently back which seems to be random. You only have to take care if you use the pre-safe as always if you do the creation not via the Django RM meaning you do a bulk insert or something, then it won't work, you have to do a find another way, but in my case it worked and yeah it was really easy and solved the problem.
Speaker 5
Good afternoon everybody. My name is Afonso. I live thirty minutes from here, so it was kind of a missed opportunity if I didn't talk today. And I work remotely for a British company called Colabra and that's my GitHub. So make files are pretty powerful, but they have a kind of arcane and weird syntax and lots of strange rules in my opinion. And so there was one day when I had like a stupid crazy idea of okay I'm going to read the GNU Make manual I don't know why I did that to myself, but I so do not try this at home. It's these slides are really hacky and ugly stuff that we can do in makefiles. But I find it interesting that it was able to do
13:25 4th Talk
Speaker 5
so much different things. So what I was not understanding at first when learning about make was that it's actually just building a dependency graph between requirements and prerequisites. So I thought instead of trying to use make for C, I'm going to try to apply it to a Python project and see what happens. So basically I can define the ru the targets for files that I need make to build. So for example in this in this example it is a really simple example Here the Python target will create a Python virtual environment, and then the install target will just read the requirements file and install it and i it has it uses one of the one
One of make features which is it only runs the targets if the pre-require requisites files have been updated. So if I try to do an install right after it has been run, it will not run because it knows that the dependencies have not been updated, which is Kind of nice. And the basic feature of make probably everybody knows. Then I started hacking stuff and things got a bit weird. So what what's happening here in this example is that okay so I can have placeholders to target. So basically with this rule what I'm saying here is that For every target that that is the whose file name is the virtual environment slash bin directory, I'm going to install that package.
And this allows us to do some crazy stuff like automatically install dependencies just based on the name for the for that target. For example, for LinkedIn formatting we probably use black i sort DJ HTML which was shown up yesterday I think and with this rule we can automatically install all of that stuff. I don't know why this is useful, it's just curiosity. Don't try, don't really use this as it is not quite clear. Also Mike is quite nice to automate some small steps or comments that we usually do during our development lifetime. Usually we would use talks or nox or py invoke for this kind of stuff, but make is native, so it's nice to be able to
quickly program this these different lines. Another cool thing also a bit weird that I discovered is that you can define default rules that make will try to match if it doesn't match any of the other rules. So for example with this with this example that I'm showing here, basically I'm alliing all of the Django manage. py commands directly to make. So it's one less word for me to type when I'm working in terminal. And this one this example here is also really weird. I discovered that you can actually embed scripting directly in a make in a makefile. So make files allows you to select your own shell So
in this case I'm I I'm I'm saying that the shell is the Python virtual environment executable. And in this in this example I thought it would be useful to have a command to automatically generate a secret key for Django using the Django function for that. So I discovered that it was actually possible to do that with make. So I did not need to create a separate script, Python script So I could just I like use three two three lines of Python directly in the make file. And this feature is also useful nowadays that we are starting to work more and more with Docker workflows So you can actually specify the shell to be a Docker command to run a container
to run a container and then you can add any code you want to execute inside that container directly in make. A bit crazy but it works and it's something that's actually it is possible. So yeah basically that's it. So if you want to if you have nothing to do you can read the GNU make manual I was just curious to try it out in the Django setup. Thank you very much.
18:29 5th Talk
Speaker 6
Thank you everyone for staying until the very end. It's very, very exciting to be here. I mean like please give a big. applause to yourselves for being here. It's so good to have you here because all of you inspire me a lot. on my journey not only in programming but like just spreading the joy of programming. That's why I also like Django Girls a lot and I'll talk a bit about it today. So as you know, Jung Girls is a free programming workshop for women. It's started by Ola
and Ola. It is all over the world, everywhere. This is just some of the upcoming events. We are at the top, middle, porto, and This is really difficult. This is us, right? No. Yeah. Okay. So tomorrow we will be with you next in next room with the sprint. So If you are a woman and if you're joining the Sprints, please do come to the next door and meet with our participants. We are going to be running the workshop from 10 to 5. just next door to the sprint. So
Actually now I'm done. This next is just we will see some photos and just celebrate because I think Django Girls is also a lot about celebrating. There is a like huge contribution from coaches, mentors Yeah, that's me, that's me. And that's my son also so it's not just a event but there is a like big tutorial behind it. It's translated into many languages and like I know it's a good
source of documentation and like a good offline tutorial. A lot of people, even though they're not women or joining Django Girls, follows and learns Django. So this is a very old map of whereabouts. It's just like you can also start your event. I think like they did a great job. We did a great job. Like there's everything you need to run a workshop and I think like this is actually a good read for anyone. to be more aware about inclusion. Like I go back to this tutorial just to remember
and remind myself like what's the best way to like communicate with the people in my team so that I can be more inclusive. It's like it's a great, great, great document, especially I think the organizer's manual has a big section about like how to be more inclusive and all and yeah something a professor in my university prepared for one of our first workshops. And the rest is just like we are going to look at some photos from a very, very old young girls and in the last one minute I'm going to talk you into it. So this Those are photos from nine Django girls workshops happened in Istanbul
between 2016 and 2019 and it's very fun. It's Guido who brought Django girls to Turkey. That professor who pre prep prepared that beautiful Django girls thing. She used to be neighbors with Guido in Netherlands And he was visiting Istanbul, somewhere after the first Europa Python Jenga Girls introduced. And he was like, when are you going to run the first workshop? I think it was 2016 sum or something like that like that December the first young girls workshop happened in Istanbul I think it was a big game changer for a lot of people
And yes. And just want to finish with a last note. Like It is impactful to help others. It's impactful to inspire. And on the way, you'll get inspired and empowered and see you next year.
23:27 6th Talk
Speaker 7
I'm sorry again it's my f first time on the stage. I'm coming here from Finland from my company Kprou. We are both very old into Django. Thanks everybody for grey dogs since zero ninety six and use it with a lot of code you see we have l lots of code written already. Kipro is one of first to employ Django like on a b on a big scale in
in SAS and we still do it But let's go into the problem. You know that we have lots of APIs everywhere, and APIs are sometimes used for integrations. And most APIs are rest and they are create retrieve update delete list and So at some point we thought that I like I thought that. It's okay to use it and
what come from it what do you actually need? First of all, you need some hacks and it's making fake model and fake query set module can be data just a data class or something like it. And query set can be just a list with some custom functions for la like f filter or something for if you need it. And then you need to make some subclasses from paginator and change list to
Actually use your API for API client functions. And then you do need to do lots of overrides in mod admin as you see lots of cats particularly those functions need to be overridden but then you get Something like this, you can probably like see an admin and suddenly you're using rest in admin. And you can of course use custom actions like but even
using normal actions You can retrieve data. In this particular app we don't do a update or create, but at least lead works and you see it's totally the same experience. So that's all. That's how we solved it and just another so solution. If you have questions Please ask.
27:05 7th Talk
Speaker 8
Hello. Hello. I'd like to talk to you about the most productive Django developers in the whole world. Maybe. And we're gonna use the incredibly flawed and biased metric of the lines of code these developers are affecting per day. Here they are. These are the most productive Django developers. And yes, some on the list, but Bruno Aller there, the number two, laid most of the ground work. So who are we? Why are we here? And are we the most productive? Or are we building something to help you become the most productive? Yes, we're building something called Django Upgrade. Django Upgrade is a tool that
automatically upgrades your Django project codes. Go from one version of Django to the next, avoid some of the grunt work. So you can install it with pre-commit, so then you can have it just running on every time you commit any Django code. If it's old stuff you copied, it will get upgraded. Or you can just install it the classic way with pip and then you can just run it on files whenever you want. You pass it the target version of Django for your code base. So 4. 1 is the latest. And what it will do is it'll run all the fixes it knows about from Django 1. 7 now and up to rewrite your code into the latest versions and not only take care of deprecations, but also And take advantage of some new features.
So let's take a look at some. Who remembers Django 1. 9? And going around your code base and adding on delete to every single foreign key and one-to-one field. It does it. There you go. There's a default. Of course it's picked Cascade, which I think was the original default, but if you needed to change it, you can It rewrites your URLs into the lovely new syntax from Django 2. 0. And if you just did a find and replace when you upgraded a 2. 0 There's a new feature pending that it finds the repathic calls and if the regex you're using matches the ones Django uses, it also rewrites it
Django two point two introduce the HTTP request. headers interface It's much better than writing request. meta. getHDP server and remembering all what that actually means. So it rewrites it. And pending release, the new Django, the new admin decorators, admin. action and admin. display, it finds all those quirky attributes you're attaching to functions that sometimes you know people insert code in the middle and they're halfway down the file and just finds them and puts them into the decorator form. A lot easier to read I've got a lovely review here on Twitter from Justin Mayer. He said thank you for the tool. Yeah, I can't take full quote Full responsibility for this.
Loads of people have contributed at this point. Yeah, so please go check out Django Upgrades. If you can test the unreleased main version, I've got a lot of stuff pending. Some someone called Thibaut Dacon has contributed a lot of the Django admin stuff recently. And please join me in Sprints. We've got ideas to improve it to even rewrite your templates for deprecations as well. So thank you.
Speaker 2
Thank you.