Latest / Talk Python To Me / #490: Django Ninja
Transcript
- 0:00If you're a Django developer, I'm sure you've heard so many people raving about FastAPI and Pydantic.
- 0:05But you really love Django, right? And you don't want to switch?
- 0:08Well, then you might want to give Django Ninja a serious look.
- 0:11Django Ninja is highly inspired by FastAPI, but also deeply integrated into Django itself.
- 0:17We have Vitaliy Kucheryaviy, the creator of Django Ninja, on the show to tell us all about it.
- 0:22This is Talk Python To Me, episode 490, recorded December 10th, 2024.
- 0:27Are you ready for your host?
- 0:30You're listening to Michael Kennedy on Talk Python To Me.
- 0:34Live from Portland, Oregon, and this segment was made with Python.
- 0:38Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:44This is your host, Michael Kennedy.
- 0:46Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython,
- 0:51both accounts over at fosstodon.org.
- 0:54And keep up with the show and listen to over nine years of episodes at talkpython.fm.
- 0:59If you want to be part of our live episodes, you can find the live streams over on YouTube.
- 1:04Subscribe to our YouTube channel over at talkpython.fm/youtube and get notified about upcoming
- 1:09shows.
- 1:10This episode is brought to you by Sentry.
- 1:12Don't let those errors go unnoticed.
- 1:14Use Sentry like we do here at Talk Python.
- 1:15Sign up at talkpython.fm/sentry.
- 1:19And this episode is brought to you by Bluehost.
- 1:22Do you need a website fast?
- 1:23Get Bluehost.
- 1:24Their AI builds your WordPress site in minutes, and their built-in tools optimize your growth.
- 1:29Don't wait.
- 1:30Visit talkpython.fm/bluehost to get started.
- 1:34Hey, everyone.
- 1:35Before we jump into the interview, I want to tell you about a massive article I wrote I
- 1:39think you'll find interesting.
- 1:40I did data science things and dug into all the episodes this year and pulled out the top
- 1:44five Talk Python To Me episodes of 2024.
- 1:48Then I did a deep dive into the content, what was covered, what was important, even learned
- 1:53a few things that I missed the first time around.
- 1:55Just visit talkpython.fm/blog, and the article is right at the top of the page.
- 2:00I'll put the full link in the podcast show notes.
- 2:02Also, if you haven't discovered it yet, Talk Python has a blog.
- 2:06While you're there, please subscribe to the RSS feed to get all the articles and announcements.
- 2:10Thanks for listening.
- 2:11Happy holidays, everyone.
- 2:14Vitaly, welcome to Talk Python To Me.
- 2:15Yep.
- 2:16Hello, hello.
- 2:16Yeah, thank you for having me.
- 2:17This is going to be the first time I've ever had a ninja on the show.
- 2:20Okay.
- 2:21Okay.
- 2:22That will be fun.
- 2:24No, Django Ninja.
- 2:24Django Ninja.
- 2:25It's going to be a lot of fun, isn't it?
- 2:26Yep.
- 2:27Yep.
- 2:27That's right.
- 2:27Yeah.
- 2:28So welcome to the show.
- 2:29We're going to talk about Django Ninja, which is, at least in my mind, you tell me if I'm
- 2:33seeing this correctly as we get into it.
- 2:36But in my mind, I see Django Ninja as Django's FastAPI.
- 2:40That's exactly like the most asked questions I ever had.
- 2:42Like, why not just FastAPI?
- 2:44So I hope I will answer during this podcast why it is, why it exists.
- 2:48And we will all work with this question.
- 2:50Ever since it first came out, I was, I saw him.
- 2:53That's cool.
- 2:54That is a cool idea.
- 2:55And I'm pretty excited to talk to you about it.
- 2:57It's going to be great.
- 2:58Before we get into all that, though, quick introduction.
- 3:00Who are you?
- 3:01What do you do?
- 3:02How do you get into this Python world?
- 3:04Yeah.
- 3:04So my name is Vitaly Kucherawe.
- 3:06Yeah.
- 3:07Good luck.
- 3:07Good luck repeating that last name for everyone.
- 3:10But yeah, that's my name.
- 3:12So I'm from Ukraine.
- 3:13I'm currently in Ukraine.
- 3:15And I'm in tech for like, I don't know, like more than 20 years probably.
- 3:19And yeah, I worked with, well, at the beginning of my career, I worked with all sorts of languages,
- 3:24technologies, like everything, like C++, .NET, Java, PHP, like everything.
- 3:29And yeah, I guess like one time I tried Python.
- 3:31And yeah, since then, this is like my go-to tool.
- 3:35And yeah, I just cannot switch to anything else.
- 3:37Yeah.
- 3:38That's how I got there.
- 3:40And yeah, like currently I work for a company, Modicos Prime, where we do AI for big pharma companies.
- 3:50We help them, you know, innovate in this area.
- 3:52And yeah, there I also use Django Ninja.
- 3:55And yeah, basically eating my own dog food.
- 3:58So that's a good sign.
- 3:59That's a very good sign.
- 4:00And it's so excellent to be able to work on these projects, but also use them with the team.
- 4:07And in reality, right, on a real day-to-day work, because I think that polishes a lot of the edges that might otherwise exist.
- 4:14Basically, I'm watching like how other people use like in a team.
- 4:17And also like I communicate with a bunch of companies who also move to Django Ninja and like are listening to what kind of struggles they have and which kind of problems they jump in.
- 4:28So yeah, that kind of helps solve it easier and somewhat more maintainable way and easier to integrate.
- 4:36So basically, that's my end goal.
- 4:38Yeah, that's awesome.
- 4:39There's a really interesting stat from the Python, the PSF and JetBrains survey.
- 4:46I'll put it on the screen for people watching.
- 4:48I think at least when I personally thought about sort of how popular is Django, I kind of always had it tied with Flask and close with FastAPI, right?
- 4:58They're kind of, if you look at the survey results, they're kind of similar.
- 5:02But what's interesting is if you actually look at what web developers use, Django is by far the most popular one, like way more popular than Flask.
- 5:1050% more or something like that.
- 5:12Not quite.
- 5:13I think it's pretty important to have a library like this around to make Django first class citizen in this sort of data exchange typed Pydantic world, right?
- 5:24This is pretty much described it.
- 5:25I think like, yeah, before this survey, like there was like Flask and Django, like, you know, there was like a tie.
- 5:32But now, like basically, I think FastAPI is eating a lot of this pie from Flask and Django.
- 5:38And yeah, you see now it kind of gets into like, you know, three main players.
- 5:43And yeah, like from what I experienced is I think it's actually like maybe developers are actually splitting into like two main groups.
- 5:51Like ones who like to use some kind of tool that have everything like Django.
- 5:55Yeah, like it can be in the Java world.
- 5:58It can be like Spring framework in Python and Django.
- 6:01While others like to, you know, start from scratch and like combine all little pieces together.
- 6:06Yeah, like take their own RM, take their own configuration, take their own, you know, file system, like everything and make it all their own.
- 6:13And this is where the market for Flask and FastAPI.
- 6:17While for other folks, it's like more like, yeah, they just want something ready.
- 6:21Like whatever issues it have, let's take it.
- 6:24It's just, you know, more standardized way.
- 6:26It kind of seems more maintainable for some people while others prefers more like, you know, replacing components, switching frameworks and do like everything custom.
- 6:36So like my vision of Django is like it should be a little bit glued to two of these worlds.
- 6:42Because what I noticed is like when people use Django Ninja, it's mainly like all the applications is actually like it's API.
- 6:51There is no form.
- 6:52There is no Django admin.
- 6:54There is no like, you know, custom views.
- 6:56You're like, so they basically tie to API and doing just the rest API.
- 7:00And I think, yeah, this is basically when it shines and when you can take like an approach like for FastAPI Flask.
- 7:08But under the hood, it will have all the batteries that comes with Django.
- 7:11So I think that's how it's, why it's actually like picking up and why it works and why people use it.
- 7:17I know there are a lot of people out there that have just some kind of JavaScript front end and a bunch of APIs.
- 7:22But there's also a lot of websites that are more server side based, more holistic, kind of like Django does.
- 7:29And having something like Django Ninja to sort of add on the API functionality when you've already got the models, you've already got everything set up.
- 7:37That's a really cool aspect of it.
- 7:39It's more interesting like in history, like why actually Django Ninja appear in Havio.
- 7:44Yeah, yeah.
- 7:45Back in the day.
- 7:45So yeah, when Sebastian released FastAPI, I was like really like mind blown.
- 7:50Like, this is like, this is all I wanted.
- 7:53Like you just write a couple of types to your function and you got the UI for, you know, for just UI for API, documentation for API, validation and everything like in one package.
- 8:04So yeah, like for lazy people like me, that was like, you know, like, yes, literally my mind blowing.
- 8:10Yeah.
- 8:11So basically I tried it out, kind of worked for me for like, you know, one project.
- 8:15And that year, like for me, the case was like, I had to integrate it with Django.
- 8:19So I had existing application with some database and stuff.
- 8:22And I just needed like simple little API to add there.
- 8:26So I decided, okay, I'll just take FastAPI for that.
- 8:30So I took it, it worked, but the problem is like to FastAPI and the hood uses Starlet.
- 8:36And there's like a little bit different approaches to like, like local storage and threads when you're basically accessing the database.
- 8:44It's not like without like special hooks and crooks to like access like database within Starlette context.
- 8:51And you can kind of lose this connection.
- 8:53And then you get just, you know, error that you cannot really debug or do something with it.
- 8:57So it was like, like for me, it was like, oh, come on.
- 9:01Like it was so, so perfect, but it just breaks in the middle of something.
- 9:05And yeah, but I tried like Django community.
- 9:08They said, yeah, it's not, it's not, it's not something wrong.
- 9:10FastAPI community.
- 9:11They said, yeah, it's, it's not something that we do as well.
- 9:14It's a Django problem.
- 9:15It's a Django problem.
- 9:16It's a fastAPI problem.
- 9:18Yeah.
- 9:19So I, and then, then I just took a look inside and yeah, basically I said, okay, just this, but literally it's really simple.
- 9:26Yeah.
- 9:27You just take input from HTTP request, you apply by the intake models validation and yeah, and that's it.
- 9:34And it works.
- 9:34So like for me, the first prototype was like done in, in a weekend and it was already like working.
- 9:39And then like rest of the time when I decided, okay, I'll try to make it a project, but yeah, to make it something that will pick up, I need to like, yeah, first make like, you know, 100% coverage and the documentation.
- 9:51So that part is actually took me like a good amount of like, almost like, you know, half a month or months of work until like, it's like presentable.
- 9:59So yeah, like the, yeah, the concept is like pretty, pretty easy, but the implementation and, you know, edge cases were like really the hardest part, but yeah, I just started, okay, I'll do it.
- 10:09And yeah, that's, that's how, how it born.
- 10:11And I guess the next part is like actually why it exists is like, yeah, so in Pydantic, there is model word is reserved for like, you know, any kind of validation.
- 10:20Yeah.
- 10:21So Pydantic uses word model for validation, but in Django world model means like tables, yeah, database tables.
- 10:29So you cannot just bring Pydantic models.
- 10:31It's the ORM, yeah.
- 10:31Yeah.
- 10:32And because people will be confused when you, okay, you speak about model and then you kind of need to think, okay, is it database or is schema?
- 10:39So like for me, the main goal was to introduce class called schema, which is like a Pydantic model under the hood.
- 10:46And then it should like hide as much as possible the word model.
- 10:50So, and like to make it like as, as much as possible compatible with, with Django and just trying to hide the word model under the hood as much as possible.
- 11:01And it kind of like, I'm lucky that schema kind of worked and yeah, because schema is, is actually how, like how Swagger defined.
- 11:09Like open, open API standard names it.
- 11:12So it kind of fits there perfectly.
- 11:14And, and yeah, that's kind of how it works.
- 11:17And yeah, then, then I was sitting like, it was, what should be the name of the thing?
- 11:21Like it was also the hardest part.
- 11:24So yeah, but yeah, I first tried it and it did like a really good performance in terms of like, you know, CPU usage and high loads.
- 11:32It was like really lightweight.
- 11:34So way faster than Django REST framework.
- 11:36So I thought, okay, she should be something fast.
- 11:38Try it, you know, word here and there.
- 11:40Okay.
- 11:41And then, and it become ninja.
- 11:42So that's how it's important.
- 11:45The naming is perfect.
- 11:46The naming is just perfect.
- 11:47You and me both.
- 11:48When I first saw FastAPI as well, I'm like, this is the way this is how it should be done.
- 11:54And FastAPI is really interesting when it came on the scene and by way of being very similar.
- 12:00So it was Django Ninja in that it, it was one of the first frameworks to say them and along with Pydantic say Python types can be something we use, not just something we run through a linter to make a better experience and the async aspect of it.
- 12:16So, and I saw that like, yeah, this is the way you should build APIs.
- 12:18And so when I saw Django Ninja, I'm like, of course Django should have this way.
- 12:23This is the way.
- 12:23The whole concept is perfect.
- 12:25Yeah.
- 12:25We just, you make a type hint and then it does validation for you.
- 12:29The linting, it does the, you know, editor auto-completion, like everything in one package.
- 12:34And yeah, like for, if you're lazy, you just thought I did one type in.
- 12:39Like before that, I was like pretty skeptical, skeptical about typing.
- 12:42Like, yeah, you just blather your code.
- 12:45Doesn't, you know, like if you just lint in it with my pie, it's like really painful to make like fully tight project.
- 12:52It's like, oh my God, it's like, it's like an X impossible to like satisfy mypy in every like, like, so Django Ninja is like mypy compatible.
- 13:02But there is lots of stuff is just tight typing or it's just some stuff is just not possible or it's like too, too blathered with, with code.
- 13:10So you have like, it's really hard to read when you fully type your code.
- 13:14So even in my day, day to day projects, like what I basically try to do, I only do type annotation for arguments.
- 13:22The rest is actually like editors do perfectly.
- 13:25So if you return some object, the editor know, okay, this is like this type or that type.
- 13:30And you don't have to like annotate what exactly does it return.
- 13:34So a hundred percent.
- 13:35That's exactly the way that I do it as well.
- 13:37I know there's some projects whose goal is to basically make Python statically typed like Instagram, for example, to try to get rid of bugs and huge programs.
- 13:45But for a lot of people, it sounds like you definitely for me, it's like I want the editor to work really well and know what it is that I can do here.
- 13:52And if I put a type here and there, like on a return value of a function, all of a sudden it gets much easier to work with.
- 13:58Yum.
- 13:58This portion of Talk Python To Me is brought to you by Sentry.
- 14:03Code breaks.
- 14:04It's a fact of life.
- 14:05With Sentry, you can fix it faster.
- 14:07As I've told you all before, we use Sentry on many of our apps and APIs here at Talk Python.
- 14:13I recently used Sentry to help me track down one of the weirdest bugs I've run into in a long time.
- 14:19Here's what happened.
- 14:20When signing up for our mailing list, it would crash under a non-common execution pass, like situations where someone was already subscribed or entered an invalid email address or something like this.
- 14:31The bizarre part was that our logging of that unusual condition itself was crashing.
- 14:37How is it possible for our log to crash?
- 14:40It's basically a glorified print statement.
- 14:43Well, Sentry to the rescue.
- 14:45I'm looking at the crash report right now, and I see way more information than you'd expect to find in any log statement.
- 14:51And because it's production, debuggers are out of the question.
- 14:54I see the traceback, of course, but also the browser version, client OS, server OS, server OS version, whether it's production or Q&A, the email and name of the person signing up.
- 15:06That's the person who actually experienced the crash.
- 15:08Dictionaries of data on the call stack and so much more.
- 15:11What was the problem?
- 15:12I initialized the logger with the string info for the level rather than the enumeration dot info, which was an integer based enum.
- 15:22So the logging statement would crash, saying that I could not use less than or equal to between strings and ints.
- 15:28Crazy town.
- 15:30But with Sentry, I captured it, fixed it, and I even helped the user who experienced that crash.
- 15:36Don't fly blind.
- 15:37Fix code faster with Sentry.
- 15:39Create your Sentry account now at talkpython.fm/sentry.
- 15:43And if you sign up with the code TALKPYTHON, all capital, no spaces, it's good for two free months of Sentry's business plan, which will give you up to 20 times as many monthly events as well as other features.
- 15:55All right, well, let's dive into Django Ninja.
- 15:58I guess people got a pretty good sense that it's like FastAPI for Django.
- 16:04If somebody asks you, what is Django Ninja?
- 16:06How do you describe it?
- 16:06What do you tell them?
- 16:07So basically like extra battery for making.
- 16:10So Django is all about batteries.
- 16:12So it's that, I don't know, like it's more like, not a battery, but more like, you know, the energy pack.
- 16:18If you need API and it's, you need to be, it's like pretty standard way.
- 16:24Like it is currently, I think like open API standard is kind of one because before there was like here and there, like the different standards.
- 16:31But yeah, like I think they kind of now become the defector and everything supports it.
- 16:36Like, yeah, like the big players, like OpenAI supports open API.
- 16:40So that kind of makes it easier to integrate with all sorts of tools.
- 16:44And yeah, there's all different UIs.
- 16:47Yeah.
- 16:47Like how you can present your API.
- 16:49So that kind of a big win.
- 16:52And yeah, like then it's like deep Django integration because yeah, like in most of the cases you define your, like if you, let's say you want to return some stuff.
- 17:01Yeah.
- 17:01Like the list of some objects from your database.
- 17:03Yeah.
- 17:04You simply just return query set and then it's validated and described correctly in your documentation.
- 17:10So yeah, like, I guess like this laziness in terms of that you don't have to write lots of stuff, make it like really perfect in terms of like, you know, day-to-day code.
- 17:20Yeah.
- 17:20Like, so you, you just, you, you just describe your, okay, this is a list of, you know, hero objects.
- 17:26And then you return like queries and that's it.
- 17:28So you don't have to very worry about like converting the data, serialization, deserialization.
- 17:35It's like in just three, three lines of code, you usually be able to like, or some kind of filter and input and output some results from the database.
- 17:44And this is, you know, 90% of business logic is usually like, okay, give me the JSON and return to JSON.
- 17:51And then you just need some kind of easy glue in between.
- 17:55I got to make one or two database queries and then here are the database objects, kind of.
- 17:59One of the things when you're talking about building this, that seemed to me to be maybe the trickiest part of it would be the fact that you have to integrate it with the Django ORM, right?
- 18:10When Sebastian built FastAPI, he could just go, well, whatever Pydantic does, I'm just going to do that.
- 18:15And here we go.
- 18:16Not to downplay the effort he's put into it.
- 18:18I know it's a lot, but he didn't also have to say, and let's make that compatible with Django models, Django ORM objects, right?
- 18:27So this integration that you put between those, so they're pretty seamless.
- 18:29That's really cool.
- 18:30Yeah.
- 18:30This is basically like whenever I tried it, then it is something that you definitely need to like.
- 18:36And it's not only like, because Django comes with like way, way, way more stuff.
- 18:41And yeah, like because what Django Ninja comes included is like the caching.
- 18:47Yeah.
- 18:47That basically fully relies on Django, like a little bit of throttling, filtering, and you know, the fields integration.
- 18:56Like, so you define some kind of columns on your database and that's automatically converted to your schema types.
- 19:02It's like, you know, so you don't have to like repeat your work two times.
- 19:07You're like define, you know, first database tables and then schema tables.
- 19:11So I think this is like, again, kind of lazy approach, but it works and it's perfect.
- 19:15This is really, really awesome.
- 19:16I don't want to make this, I don't want to put you guys, your project in sort of competing against another project.
- 19:24But for people who are using the Django REST framework, you know, why should they look at or what benefits do they get from Django Ninja?
- 19:30It's more like, you know, a stealth player that, you know, takes majority.
- 19:35Like, I think Django Ninja is still like, you know, was it like 5% or something like that on the latest polls?
- 19:41But yeah, so it's, it's, it's really big industry and Django like REST framework has already like lots of plugins or like tools or like, you know, community that you can rely on.
- 19:52But yeah, again, like the, the, basically, I think the problem that is solved with like this Pydantic validation and serialization is like, yeah, you simply rely on like latest standards.
- 20:04Yeah, like, so if you never use like type annotation, this is actually first, the good way to try it out and see the benefits.
- 20:12Yeah.
- 20:13Like, so you don't go like, you know, full 100% typed code, but you just lightly type it when you just need it for API.
- 20:21And then you already can see, okay, what's, what's the benefits and how, how you actually can perform better in terms of like making less mistakes, like, you know, typos or like type conversions.
- 20:34So like the main benefit is like, it was just, so you define the type input, like from some very complex object that your API inputs.
- 20:43And then you have like runtime guarantee that whatever type you wrote there, you can just rely it.
- 20:49You have guarantee that if you define it as an integer, then there will be like a string or I don't know, like a flaw there.
- 20:55So you, you, you have this guarantee and then you, it kind of brings away from you this validation hassle.
- 21:02So you can kind of be more relaxed in terms of, okay, yeah, my data is valid and I can work with those types without worrying about validation or, or serialization of the, of the, of those fields.
- 21:15As you were describing that, I was thinking, think of all the value that Pydantic adds to working with JSON data.
- 21:21You get that for your API at the boundary, right?
- 21:23With no effort.
- 21:24That's pretty amazing.
- 21:25Yeah.
- 21:25Pydantic is just, it's so good.
- 21:26What about performance?
- 21:28I know you have a, a graph, which I love it.
- 21:32I never actually like remeasure it again, but that's how it worked at the beginning.
- 21:37So I don't think like back in a day when this was introduced, I'm not sure if currently a FastAPI or I mean, Django REST framework have any like async support because back then it, it was not.
- 21:49So you can, you can, you can, you could immediately have a boost on your performance.
- 21:53If you just, you know, free from database.
- 21:55So you can just return your, your query set as a sync.
- 21:58And then you rely on, you know, event loop to pull those data and return it back to the clients.
- 22:03And it was like really for, for some project that I've communicated with like other companies and they, they like, they saw like a performance, something like that.
- 22:13Or like their CPU usage was like a, you know, load average about like 50% drop to like 5%.
- 22:20Yeah.
- 22:20So that was like really a immediate, immediate boost up, but it only works for you.
- 22:26If you're like, yeah, for, for like very simple case when you just need to parse data, insert database, return, return the result.
- 22:32So this is like where it's like performance just, you know, it can be like five to 10 times you can get this improvement.
- 22:39Yeah.
- 22:39And people want to see the graph.
- 22:41They can just visit django-ninja.dev and it's right there on the homepage.
- 22:45But yeah, if you start doing like you start, you know, doing some kind of square roots of some numbers, yeah, you will not get this performance.
- 22:52You will have to, again, rely on number of processes or threads or stuff like that.
- 22:56Did you do this analysis with Pydantic 1 or Pydantic 2?
- 22:59This is Pydantic 1.
- 23:01Yeah.
- 23:01So I never actually measured like what is the performance.
- 23:04So yeah, like, but again, it's like probably like you will see the difference with Pydantic 2 when you like really, so you're in, you have inputs of like really bulky Jasons.
- 23:14Like, you know, let's say you have like, you know, payload that is like one megabyte and you will, you will definitely see performance.
- 23:23But yeah, it was just small, small inputs.
- 23:25Yeah.
- 23:25It will not be that dramatic.
- 23:27I see.
- 23:28So if it's just a small input, then Pydantic is not the main part of what you're doing.
- 23:33But if it becomes large, then the parsing becomes a big part of it.
- 23:36Yeah.
- 23:36Yeah.
- 23:36There is also like one of the features of Django Ninja.
- 23:39So you can replace your, your parsers and serializers.
- 23:43So you can say, okay, you know what?
- 23:45Let's use or Json for inputting out.
- 23:47And again, you can, you can get some extra performance just because you have those parsers are quicker.
- 23:52They have their own quirks.
- 23:54Maybe not some, something like not standard, but for the, for the most cases, it just, you
- 23:58can increase your performance with just, you know, adding a few configuration lines.
- 24:03Yeah.
- 24:03Yeah.
- 24:04Yeah.
- 24:04That's really interesting.
- 24:05If you know that whatever difference those other faster parsers might have, it doesn't apply to the data you're exchanging.
- 24:10Just use them.
- 24:11Right.
- 24:11Yeah.
- 24:12What about uvloop for the async side of things?
- 24:14The thing is like, it's really like Django Ninja has nothing to do with it.
- 24:17It's fully relies on, on Django itself under the hood.
- 24:21So like, yeah, basically Django starts, is GI process, do some stuff on top, you know, and then gives you basically back to Django, a final context with request.
- 24:33And then you, you work on it.
- 24:34It's probably down to the production app server that use like gunicorn or Hypercorn or, or whatever you run it on.
- 24:43Right.
- 24:43Like that thing you just specify that runs on uvloop and then off it goes.
- 24:47Yeah.
- 24:47Yeah.
- 24:47Yeah.
- 24:48I'm pretty interested to see what would this graph would look like with Pydantic two, because that's the one that was rewritten in rust, which I had Samuel Colvin on to talk about a time or two.
- 24:56That's, that's fun.
- 24:57So let's see, let's talk through, give us a sense of what programming this looks like.
- 25:01And, you know, just keep in mind that, you know, not everyone can see what we're talking about, but give us as much as you can, sort of a, a feel for how to write code in Django Ninja.
- 25:11You import Ninja API from Ninja, and this is your like main kind of API instance.
- 25:16And so you, you create this instance, like API, your Ninja API, and then using the decorators, it was just API.get.post.put, like whatever you, method you want.
- 25:28You just decorate some functions and then inside your functions, you are, you know, you do your business logic.
- 25:34But the cool part is that you can define input parameters, which are basically gathered from your request.
- 25:42So if you do define a couple variables type in, they will, by default, they will come from like query parameters.
- 25:48If you define like more complex object, it will come from like your body and it will be parsed from, from JSON.
- 25:55And yeah, like you define those functions, you include it into your URL patterns.
- 25:59That's, that's it.
- 26:00You have your, your ready API.
- 26:02You don't have to like do like, put it into installed apps or like configure any settings.
- 26:07So yeah, like my goal is also to create as simple as possible, like integration.
- 26:11So just in two, three lines, you're ready, you have already API.
- 26:16And yeah, that's, that's basically like did the whole, the whole, the whole thing.
- 26:20That's it, right?
- 26:21There's not a whole lot to it.
- 26:22There's also like for, I guess for, for the difference for folks who work with FastAPI.
- 26:27The key difference is that Django Ninja, Django Ninja's operation, their function, the first argument is always request.
- 26:35Because this is like historically what Jenga, what Jenga protocol is.
- 26:39Yeah.
- 26:39Like your, your first argument to your view function is request.
- 26:41And yeah, they, they rely a lot of stuff in their request can have user, you know, extra information like middle layers can access, you know, some kind of attributes.
- 26:51Okay.
- 26:51So this is like kind of important.
- 26:53And this is like also like the key, the key difference or requests is request is always there as a first argument.
- 26:58And yeah, like there was a suggestion, so let's remove it.
- 27:02But I think, yeah, we should keep it, we should keep it consistent with, with, with default Jenga view.
- 27:07And yeah, like, I think that, that, that, that works perfectly.
- 27:09I don't remove it.
- 27:10I like it.
- 27:10I think, I know FastAPI is optional to have the request or not have the request, which, which is not a bad choice.
- 27:17I just not too long ago converted the talk Python website into court, which is async flask with the goal of converting the course of websites and a bunch of other things that I have going over as well.
- 27:30And in that world, you know, you just have this thread local request object.
- 27:36And that always just seems like breaking the rules to me.
- 27:38It's like, you know, what if I want to run this, another thread, how do I capture it and send it?
- 27:42I just like having the object just passed in is nice.
- 27:45I think I like it.
- 27:46I think it's a clean design.
- 27:47Yeah, it's like clear or like more open.
- 27:50Yeah.
- 27:50So you can always know, okay, this is something about, about the HTTP request.
- 27:54So it's always there.
- 27:54A little easier for testing and reasoning about it.
- 27:56It's not like, well, if I call a function, which calls a function, which needs the request, is it safe for it to just grab the thread local request?
- 28:03Or, you know, like there's a lot of ambiguity in here.
- 28:06It's just like, yeah, here's the request object and go with it.
- 28:08So that I'm, I'm, I'm a fan.
- 28:10I'm a fan of it.
- 28:11Another, another thing that's really nice, and you hinted at this before, is that it has automatic documentation, very, very similar to FastAPI for what you write without you doing any work, right?
- 28:23You just go to slash API slash docs, and you get this nice UI, right?
- 28:27You don't have any hassle of creating some UIs.
- 28:30You just, you just have it for free, right?
- 28:32So you just go to slash API slash docs, and there you have it.
- 28:35And you can already like, so by default, Django Ninja comes with two, like three doc and a swagger.
- 28:42But there is technically like pretty easy to integrate any other tool that can work with, with open API specification.
- 28:50And yeah, that's, that's the way you can like fine tune or like make it nicer, easier, or like, you know, do some kind of custom stuff with your documentation when you share it back to like your, your clients, your users.
- 29:02This is a very Django like feature as well.
- 29:04I mean, I know it first probably was seen in FastAPI as an automatic thing, but you know, Django, you just create your models and you get the admin section and you get all these other things around what you've created.
- 29:14And this is sort of, Hey, you just get this documentation and this testing tool for exploring your API just by creating it.
- 29:21Basically it was Django who first sort of, yeah, we just give you admin for free, just define a couple of classes in there.
- 29:27And I think like, I think the Django REST framework was also like the first who like gave you the UI also for free.
- 29:34So you just define your serializers, you create your classes stuff, and you have UI that you can test.
- 29:43So I think like the UI testing is like one of the most kind of best debugging tool you can, you can have.
- 29:51Yeah.
- 29:51So you like basically like I use it like day to day.
- 29:56They are like define your function.
- 29:57I just try it out on three UI and that's it.
- 30:00That's like very, very, very helpful.
- 30:02You can turn it off.
- 30:02Right?
- 30:03Yeah, sure.
- 30:03It's really nice for, if you're making a public API, it's nice to just have it.
- 30:07But if you're making an API that is internal, but your website has to be public, you might not want to publish the API just to the world if it's not meant to be a public API, right?
- 30:17There is an option to define your custom authentication for the docs.
- 30:21You can define, okay, docs, doc outs or something like that.
- 30:24You can say, okay, only like, you know, Django super users can access this documentation.
- 30:29So it's also like a pretty common request or feature that people use.
- 30:33And as well, you can, yeah.
- 30:35So I guess one of the interesting part that, that also differs from FastAPI.
- 30:40So you can create like, you know, as, as, as many as possible ninja API instances.
- 30:45So you can have like slash public API slash private slash custom slash, yeah, whatever, like version one, version two, like, and basically you can define multiple, multiple versions, reuse functions.
- 30:57And then, you know, can continue your work while keeping the old, old, you know, version one, version two code or private public, stuff like that.
- 31:05So that's, that's, that's also way that people use it and having problems configuring.
- 31:11So that's what I see from issues.
- 31:15This portion of talk Python to me is brought to you by Bluehost.
- 31:18Got ideas, but no idea how to build a website?
- 31:22Get Bluehost.
- 31:23With their AI design tool, you can quickly generate a high quality, fast loading WordPress site instantly.
- 31:29Once you've nailed the look, just hit enter and your site goes live.
- 31:33It's really that simple.
- 31:34And it doesn't matter whether you're a hobbyist, entrepreneur, or just starting your side hustle.
- 31:39Bluehost has you covered with built-in marketing and e-commerce tools to help you grow and scale your website for the long haul.
- 31:46Since you're listening to my show, you probably know Python, but sometimes it's better to focus on what you're creating rather than a custom built website and add another month till you launch your idea.
- 31:56When you upgrade to Bluehost cloud, you get 100% uptime and 24 seven support to ensure your site stays online through heavy traffic.
- 32:05Bluehost really makes building your dream website easier than ever.
- 32:08So what's stopping you?
- 32:09You've already got the vision.
- 32:10Make it real.
- 32:11Visit talkpython.fm/bluehost right now and get started today.
- 32:16And thank you to Bluehost for supporting the show.
- 32:19When I saw this feature, I thought, oh, this is fantastic.
- 32:24I really, really love it.
- 32:25So let me give people a sense of how this works.
- 32:27You create an API instance, a little bit like Flask or FastAPI, and use it as a decorator, like at api.get.
- 32:34And you give it the URL, right?
- 32:35So what you're saying is I can create an API 1 and API 2, specify a version on it, which will be part of the specification, and then register both of those under different bases.
- 32:47So like in the example in your docs, you have registering API slash V1 slash an API slash V2 slash.
- 32:54And then you can have different implementations, but by just doing this little trick, you can have them both in production at the same time.
- 33:02And I think that's pretty awesome.
- 33:03Yeah.
- 33:03And this is like actually like on practice.
- 33:05I saw that a couple of times here when people like move to some newer version.
- 33:09They just copy like entire API, I think, change a couple of methods, say, okay, this is version 2, and you're ready to go.
- 33:14So they don't have to like rely on some kind of external routers to route between different versions.
- 33:21So they can keep two versions in same code base and don't have any problems with it.
- 33:25I think one, this sort of UI pattern or this URL pattern, I think you've got to have a little bit of foresight to have API slash V1 slash to start with, right?
- 33:36But I mean, I guess you could always add slash V2 slash V3 afterwards.
- 33:43But if people think ahead a little bit and put versioning in there explicitly, that would be nice.
- 33:49Now, do you have, can you, if a function doesn't change across APIs, can you put, you know, API 1 decorator get and API 2 decorator get onto the same function?
- 33:59Or do I have to kind of do some redirect, indirection?
- 34:02That part is probably will not work.
- 34:04And I think like, so probably someone next releases.
- 34:07So inside API, you can also define a couple of routers.
- 34:11Yeah.
- 34:11So this is kind of, so you can split your application API into like different modules, which called routers.
- 34:17And yeah, so basically the idea is that you can define, you know, one router with a couple of methods and then use it in version one and version two.
- 34:25So that's the idea, which is actually, well, it's not ready yet, but this is something that is coming in next release because this is also like very, very requested feature.
- 34:34So you don't have to even copy your version in code by just reusing stuff that is common and then just overrule the new methods or the new parameters that land it on new version.
- 34:46Yeah, I really like it.
- 34:47That's great.
- 34:48Well, yeah, this is that this is an amazing feature.
- 34:50When I first saw this, I thought, oh, yeah, this makes version in your API so simple.
- 34:54And you even get documentation with it just because that's how documentation works, right?
- 34:59Yep.
- 34:59And you pointed out that you can have public and private APIs in this similar way.
- 35:04And so you could have public APIs, maybe multiple versions of them, but also a separate private API that potentially exposes more more things.
- 35:13Right.
- 35:14And as you said, you could even put authentication on top of it separately.
- 35:17Right.
- 35:17Yep.
- 35:18I love it.
- 35:18Let me go back to the homepage.
- 35:20Let me ask you a question here.
- 35:21So you said this is another thing that I think I'm a big fan of.
- 35:24So in your first example, you talked about, oh, if you put parameters into your API and you give them types, they'll be parsed and validated and all that.
- 35:31But like FastAPI, you can define a Pydantic model that is the type of the thing.
- 35:38And then the values of that Pydantic model will be validated and populated from different areas.
- 35:43Right.
- 35:43So you could have like a customer object or here you've got an item which has a foo and a bar of different types as the parameter to the API.
- 35:51Right.
- 35:51This is really key.
- 35:52So you define it once.
- 35:53Yeah.
- 35:54Or maybe you can even define it on a model level.
- 35:56So you create your table, you know, customer name is string, you know, birth date is like daytime.
- 36:03Then it can be automatically transferred to the schema, which is Pydantic model under the hood.
- 36:08And then you call it pass it as an argument.
- 36:11And yeah, you have your data validated.
- 36:14It automatically parsed through JSON, extracted, validated.
- 36:18And you're not only limited to JSON.
- 36:21So Django Ninja also comes with what is called parsers.
- 36:25So you can define, okay, is it a YAML parser, XML parser, CSV parser.
- 36:30So you can define your own parsers.
- 36:32And then you can just have different inputs.
- 36:34And yeah, they will parse it.
- 36:36But yeah, like the core guarantee you have is that your annotated argument that you pass here will always have your log.
- 36:44If you have name as a string, it will be string.
- 36:46If your birth date is date, then it will be date.
- 36:49And you have this kind of guarantee that you can always rely and be sure that it's parsed and validated.
- 36:55Yeah, that's a big point of this, right?
- 36:57Once the function gets to where your code actually runs, it's good.
- 37:01The data has already been validated.
- 37:03Everything, right?
- 37:04Yeah, you focus on business logic.
- 37:06You put aside your mental context about validating stuff.
- 37:10You're like it's somewhere else.
- 37:11Someone else defined it.
- 37:13So you don't worry about it.
- 37:14You just, in your operation, you focus only on business logic and that's it.
- 37:18That's super interesting.
- 37:19And the reason I focus on this is you said that the conversion of input data coming in to the Python model can come from a bunch of different locations.
- 37:28JSON, path parameters, query parameters, cookies, headers, forms, etc.
- 37:33Will it take from multiple locations to fill this out?
- 37:36For example, will it take the body of that JSON and maybe pull the name out of it, but then get the ID from a path parameter or a query parameter?
- 37:46Will it combine it in this way?
- 37:48Yeah, basically, like the rules are pretty simple.
- 37:50So you can actually mark.
- 37:53So there is like your import from Ninja, like, for example, form or header.
- 37:59And then in like in this square containers, you can define, okay, this comes from form, let's say.
- 38:05And that way you can tell, okay, it's just, okay, you parse the data only from form data.
- 38:10Don't just try to parse it through JSON and stuff like that.
- 38:13I guess you can search in documentation, try like searching for forms.
- 38:17That will be a good example.
- 38:19Form data, yeah.
- 38:21So you see, you kind of define, so we import form container.
- 38:24You say, okay, username is a string that should come from form and password is also string here that comes from form.
- 38:31And that way you can just leverage on default, you know, for multi-pal from data or just default form data.
- 38:38And yeah, like simply parse it without JSON, but just the raw form data.
- 38:43So you can say that for individual values, like if you're passing in the username or password.
- 38:49And you can even do that for your Pydantic models, which is cool.
- 38:53You say the item, instead of being of type item, you say it's of type form of item, which means go to the form and get it, right?
- 39:01Yeah.
- 39:01You must have done some pretty challenging type stuff to get this all to work, right?
- 39:06To get it.
- 39:07So how do you specify it comes from the form?
- 39:10But also that when I say item dot, it gives me lists from items, not from form.
- 39:15And right.
- 39:15Is this like an annotated thing or how does this work?
- 39:18This is actually like, like really kind of hack away.
- 39:22Like, yeah.
- 39:22How to make it work.
- 39:24And then the first, yeah.
- 39:25Like editor should do that to complete.
- 39:28mypy should not like scream at you and it should validate under the hood.
- 39:33So there's like a pretty, pretty like nasty kind of if typing stuff, annotations.
- 39:40So it's like really like inside it looks ugly, but outside it's like perfect.
- 39:46Yeah.
- 39:46So you get like, you just define, okay, it's form from item.
- 39:49Your editor works, your serializer works, everything works, but under the hood, it's like pretty,
- 39:54you don't want to maintain, maintain this code.
- 39:57It's like too much stuff to satisfy all parties, the my pie, the code and the editors.
- 40:02That's exactly what I was thinking.
- 40:04It looks simple, but I bet it was, I bet it was tough, but it's really nice.
- 40:08Yeah.
- 40:08It's like the differences between 3.7, 3.8, 3.12.
- 40:13Like they're doing, you know, every like year the new Python works and then there is like
- 40:17little tweaks here and there.
- 40:18So it's like, yeah, it's like the simplest to keep it simple.
- 40:21Like it's, yeah, it goes like a lot of testing on, on, on, under the hood.
- 40:25That testing is probably pretty hard.
- 40:26I would imagine because you've got different consumers, you know, how do you make sure that
- 40:30an editor works and that mypy works in the code runs, you know, this is not just maybe
- 40:35a unit test, a simple unit test.
- 40:37I imagine.
- 40:37Well, yeah, I remember when I started like, yeah, like I just, I thought, okay, I'll just
- 40:41make a 100% coverage on all Python versions.
- 40:44But yeah, that was like really tricky part because yeah, there was like edge cases for, for some
- 40:49combinations.
- 40:49So if you know, maybe like a Jenga plus Python 3.6 or stuff like that was like really giving
- 40:55some headaches.
- 40:56But I was at some point lucky that first Sebastian did it for FastAPI.
- 41:01So I literally just copied lots of test cases from FastAPI codebases, just a little bit
- 41:08adapted, and I already had a pretty extensive test coverage.
- 41:11So then I just focused on specific implementations of here and there.
- 41:16But yeah, the end goal is really the usage should be really simple.
- 41:22So your editor works.
- 41:24If you use mypy, you really like types, you have 100% type correction.
- 41:30And then on the hood, it does as fast as possible, whatever, like Pydantic Magic,
- 41:36media with Rust.
- 41:38It also does it as fast as possible, currently possible with current technologies.
- 41:44I'm going to go talk through the motivation section because you've got this motivation
- 41:48page that I think is probably a really good place for people to start as they're considering
- 41:53this.
- 41:53There's a lot of interesting pieces here.
- 41:55For example, some stuff that we haven't touched on like dependency injection and other really,
- 42:01really wild ways to sort of combine code to make it cleaner and more single purpose functions,
- 42:09right?
- 42:10So we talked about the Django Ninja will take inputs from these various locations, validate them, convert them,
- 42:19then pass them in.
- 42:20But this example you got here.
- 42:22You're using external functions to say get a user and then using that as part of the population,
- 42:29the data, I guess.
- 42:30Tell us about this.
- 42:31Basically, this is how FastAPI works.
- 42:33So they really define all like arguments there.
- 42:37And if you like, let's say you need a database in your function, like if you're using like SQL
- 42:42Alchemist, then you're kind of forced to have this DB engine as dependency or like user as a dependency.
- 42:50And because, you know, with Django, you kind of already have it either like you don't have like to access database,
- 42:57you just use models dot object and you don't have to worry about.
- 43:01So this part is for all the way.
- 43:03And as well, like users also like stuff that by Django protocol, you kind of have it on a request.
- 43:10Yeah, request that user or request that session.
- 43:12There's lots of stuff you already have in kind of like settled way and that people already expect it to be there.
- 43:20So that's why like, yeah, like I was wondering, should Django Ninja have dependencies?
- 43:25But my vision, it should not.
- 43:27It should be like a bit more simple and rely on the current way of work where people are already know,
- 43:33well, okay, where's my user?
- 43:35Where's my session?
- 43:35Where's my cookies extra if I need to?
- 43:37So you worry only about like your arguments are the stuff that should be validated,
- 43:42not the stuff you depend on.
- 43:45Yeah, like, you know, database or like some kind of authentication.
- 43:48And yeah, that's why you can also separate the logic.
- 43:52So because if you already like entered the execution of the function,
- 43:55that means all the process that was before is already done.
- 43:58You have guaranteed that if you define authentication,
- 44:00that's already done and you have your user.
- 44:02If you like relied on some kind of validation, then it's already there.
- 44:07So again, your focus is only on business logic of application and the rest pieces should not be involved.
- 44:14And don't, you know, you don't bother with them at all.
- 44:17This is super nice.
- 44:17So let me try to give people a sense of what I'm talking about here over audio.
- 44:22And so in the example, like I said, I'll link to the motivation page is you've got this function that needs the database.
- 44:30It needs the current user and it needs some arguments.
- 44:32Right.
- 44:33So the argument is handled by just what comes into the posters, part of the URLs and stuff.
- 44:39And then there's a database and you can say this depends on a function, which then Django Ninja will call automatically.
- 44:45And you can say the current user also is a user and it depends on this other function, which actually is an async function and being called for, which is, that's amazing.
- 44:54This is how FastAPI works.
- 44:56So it depends is actually not the part of Django Ninja on purpose.
- 45:01So this is like example how it is in FastAPI, but it is not on Django Ninja because it's like all of the building.
- 45:07This is the Django, this is the, this is the FastAPI version.
- 45:10Yeah.
- 45:11Yeah.
- 45:11And so you're saying, no, this is what we do instead.
- 45:13Okay.
- 45:13Got it.
- 45:14Yeah.
- 45:14You know what?
- 45:14I'm not a big fan of dependency injection.
- 45:17Not necessarily the arguments are fine, but this, the whole idea of dependency injection, I always find it looks cool in practice.
- 45:23But then when I'm trying to work with it, I'm like, where does this come from?
- 45:26Like, why do I have this?
- 45:27And how do I, how do I get back to it?
- 45:29You know, I have kind of the same feeling about like any, any like Python approaches to the dependency injection libraries,
- 45:36because I think the Pythonic way is actually like, like it's more kind of, because you can import from strings.
- 45:43You can, you know, pass kind of any type of arguments and just access them by like the, this quarks.
- 45:49So you, I think you never should need like, you know, Java, Java world dependency injection, because the language itself kind of gives you a way to not call it a dependency, but find kind of verbal work around how to access, you know, one type of class or another.
- 46:07So you do, so you do, so you don't even call it like in, in, in, in dependency injection while it, you actually like inject it from some configuration.
- 46:15So like, basically what I try is like, you know, like the, the, I guess the perfect libraries that exist in like Python is like, let's say like requests, right.
- 46:24They could have done like the same way.
- 46:26Like, okay, like let's do dependence injection or some kind of SSL protocols, like, you know, timeouts and stuff like that.
- 46:33But they, they really try to like make it as current Pythonic way as possible through like some quarks or like, or, or like, you know, stuff that you, you never, you never know.
- 46:42Okay.
- 46:43What's what's actually injected, but verbally, you know, what, what's, what's going on.
- 46:47Yeah, the quarks and the stargs, quarks, all that stuff is when I see that it's the function signature, I feel like, okay, the, you kind of given up on helping me know what I'm supposed to do here.
- 46:57The only way to know is to go read the documentation, which not a big fan.
- 47:01Let's talk about authentication.
- 47:02So what's the Django Ninja way for authentication here?
- 47:07It looks pretty integrated.
- 47:08It's kind of both integrated and it's pretty like loose and flexible.
- 47:13So yeah, even, even, so, you know, you just on your ninja API, you define like auth variable.
- 47:19So it's purposely not, not called authorization or authentication.
- 47:23It's just auth, you know, so you can define yourself.
- 47:26Do you want to implement authorization or authentication?
- 47:28It's like really up to you.
- 47:29Okay.
- 47:29Because it's like, yeah, you just, the auth is like a function that will be called with your request.
- 47:34And then you can kind of define, you can read some kind of cookie or token or like, you know, whatever, like it's like up to your business implementation.
- 47:42And yeah, if it, if it's, if it's return something, then this is like a, you know, your, your, your object that will be attached to request.
- 47:49Dot.
- 47:50Off inside this function.
- 47:51You can also access request and like attach your request.user, which is what is in most cases are done.
- 47:57If you like, you have user in your system that is tied to some token or stuff like that, but it's kind of on purpose that yeah.
- 48:05Like it's, it's not always, you have user.
- 48:06So maybe you just need some kind of object.
- 48:09Maybe you have API keys.
- 48:12Client or yeah, whatever.
- 48:14Yeah.
- 48:14Yeah.
- 48:14Maybe you just need to pass an API key.
- 48:16It's not tied to someone.
- 48:17Yeah, for sure.
- 48:17I tried not to like force people into like deciding what exactly they're implementing.
- 48:22They just need to implement a function and then use it.
- 48:26But yeah, by default, you can just import default Django auth that will use like your standard Django session based authentication.
- 48:35And yeah, just live with that by some default cookies, tokens.
- 48:39So like whatever you prefer.
- 48:41What about async?
- 48:41I think it was like one of the core features.
- 48:44Yeah.
- 48:45Like, and what was like different from Django rest framework.
- 48:48And yeah, it kind of, yeah, it does.
- 48:50Like I see like from the feedback from companies who use like it is sometimes like truly help them like to, to take a like some kind of slow, slow request that they're like just to waiting for some data.
- 49:02Yeah.
- 49:03But yeah, the thing is like you're not, you are not required to use one or another.
- 49:08So you can pretty much mix both worlds in your, in your code.
- 49:12So, because, you know, it was Django because initially Django was not designed to be, I think, friendly.
- 49:18So, and when you, for example, like, because let's say you, you request a user return, but under the hood, let's say it's called a user dot group, which is also will initiate another request to the database under the hood.
- 49:31So this is, this part is like, it's really hidden from you and you cannot really sometimes be involved in manipulating.
- 49:38And so that, that request also gets into async context.
- 49:42So yeah, the simplest way you just can, for some complex stuff, you can just mix two worlds, you know, async and not async functions.
- 49:51They decide what you need.
- 49:52You need like some kind of a weight performance while waiting for some, you know, network connection database, or you just need like, you know, to make it simple.
- 50:00Simple, easy.
- 50:01And you just don't worry about if it's, you know, that does some kind of a little bit of weight.
- 50:05And so mom, I would say my, my go to approach is like, yeah, basically I do all the stuff in sync mode.
- 50:13It's only when I really need to maybe like server side events or like, you know, a weight into some network connection or like long database queries that returns lots of results.
- 50:22And yeah, this is, this is like, you just switch to async and you just await your result and you win some, some performance out of it.
- 50:31But by default, I would, I would just go with no, no, no, no, no, no, no.
- 50:35I think way.
- 50:36Yeah. I would as well.
- 50:37Although for my app, at least I found most of the time there was some database call or there was something going on or like, all right, this part is async.
- 50:45So, you know, work your way back and make the view function async as well.
- 50:48But if you don't have to, then certainly don't, right?
- 50:50It just adds complexity that you don't necessarily need.
- 50:53The reason I'm asking is what's the story with the Django ORM in async these days.
- 50:58I know that this is kind of one of the last parts for the Django async support journey.
- 51:03I think they settled on what it is now.
- 51:05So you can like, there's extra methods on ORM, like a get, a create, a like filter or stuff like that.
- 51:12So you can already like do a bunch of default stuff.
- 51:16All right. So those you can await already. And that should be.
- 51:18It's a tricky part when, you know, you, let's say you have a query set, so you like awaited it, but then inside you, you do, you do some kind of loop and then access some, you know, dot, dot something that is actually foreign key or many to many field that.
- 51:31So then you need to like, keep in mind that you need to like do, do the join.
- 51:35Yeah. Like already in your ORM query, like, you know, select related or pre-fetch related that will do for you this, this pre-fetching.
- 51:42So then on the time when you're accessing some attribute, it will not, it will not perform like a query for you because this is actually like what's, what's actually biting Django ninja users.
- 51:53So yeah, they, let's say they're creating like schema that outputs user and a group.
- 51:58Yeah. Like again, a group is some kind of schema.
- 52:00So they just, okay, I'll just return the async query set and yeah, be, be ready with it.
- 52:05But then Django kind of, and I think it's probably good that they, they, they tell you like, okay, so you're accessing the attribute that requires the database.
- 52:13So you kind of get an error.
- 52:14So they kind of force you to think, okay, you know, if you want to really get, get it correctly, then you just better like evade with, with joint.
- 52:28So that's good advice for people, even if they're not doing async, right?
- 52:31Right.
- 52:32This, this N plus one problem can make your ORMs very slow.
- 52:34And if you know, I'm going to go access this thing.
- 52:36That's a foreign key.
- 52:37Maybe this means like, if you use it by default, you can kind of catch those, those like parts that you were not a word before.
- 52:44And then you kind of, they will bite you before you go to production.
- 52:49So maybe that's, that's the way to go.
- 52:51Some people will like this and some people will not like it.
- 52:53But yeah, it will definitely let you know what's going on.
- 52:55Right.
- 52:56Amazing.
- 52:57Anything else before?
- 52:58I guess one thing really quick here is when you say, when you're doing the async stuff, can I just use the Django admin management tool?
- 53:06Or do I have to run with an async server separately?
- 53:10What's the story there?
- 53:11It's just work out of the box.
- 53:12So yeah, you just run it under you, you record like any other SGI worker and you have your, your, all your, all your default stuff will be there.
- 53:20So basically it's like, it's out of the box.
- 53:23It should work.
- 53:24Like, you know, it's, it's, it's, it's all, you only have tricks only was, you know, this N plus one problem.
- 53:29So this is where you will have to keep, keep, keep attention and make sure it's like a weighted properly.
- 53:34Yeah.
- 53:35uvicorn is really nice.
- 53:36They're making a lot of progress.
- 53:37They're now a standalone server.
- 53:39You don't have to use them.
- 53:40You don't have to say Gunicorn with uvicorn workers.
- 53:43You can just run it as its own proper Linux server and so on.
- 53:47It's really nice.
- 53:48All right.
- 53:49So that's what brings up.
- 53:50Is there anything else you want to tell people about Django Ninja that we, we should cover?
- 53:53I think I would like to talk about the, the, you know, so currently we are in this LLM era where, you know, people no longer, you know, Google or like go, go to documentation.
- 54:03They just simply ask your, you know, favorite OpenAI or Anthropic chat to like write you some code.
- 54:10So what, what I currently notice is like, you know, when library do some kind of new iterations, you're like introduce new stuff.
- 54:18Like, you know, make some kind of, you know, maybe even breaking changes.
- 54:21You know, the LLMs will not sexually be out time with, with those changes.
- 54:26And that is actually like bringing lots of problems.
- 54:28Like, so like the, with issues that I see, like I see that people just, you know, you're working on recently, but probably they, they asked LLM to implement some feature that is already like old syntax and already like, you know, giving in some problems.
- 54:42So it's really, you know, this problem for like libraries that, you know, want to make changes, but on the other hand, like LLMs will pick it up, you know, maybe in like year best.
- 54:55Yeah.
- 54:55Like whatever new version, like I saw this a lot with Pydantic when they, you know, they already like a version two for like a year in a while.
- 55:04While if you ask LLM to implement some stuff, they, they still giving you the old, old approaches.
- 55:09So I think this is like, maybe like a, something it's not to open the developer software and source community, the way to influence it.
- 55:17But yeah, maybe like, so what I, what I am currently thinking is like adding some kind of tooling on top of, or documentation that will just, will give you the latest, latest result.
- 55:29Oh, that'd be cool.
- 55:30Yeah.
- 55:31But if you like listening to this podcast, you can go to LLM.jangoNinja.dev.
- 55:36It will, it will, we'll redirect you to some ticket where I will notify when it's ready.
- 55:40So like for now, we just redirect to, to GitHub issue.
- 55:45I will probably need to write better access, but here you can subscribe and I will just try to publish some kind of a better documentation tool in this LLM world where you can ask, you know, a question.
- 55:57And it will answer you latest, latest and better approaches without like, you know, forgetting all the old knowledge and giving you the newer updated version.
- 56:08So that's, this is a serious annoyance with LLMs because you'll ask it a question on how to do this thing.
- 56:14And it looks like an amazing answer.
- 56:15You're like, Oh my gosh, save me so much time.
- 56:17And then you'll try it and it'll, it'll be using a function or a class or something that doesn't exist.
- 56:22Like, Oh, you're lying to me, please.
- 56:24This doesn't exist.
- 56:25Oh, you're right.
- 56:26Here's the real way.
- 56:27And that's, that's even before you get to it being trained exactly on the docs.
- 56:31So are you using some sort of rag local LLM or what's, what are you doing here?
- 56:36Generally, like my vision is like, yeah.
- 56:38So this tooling should give you like some kind of regular latest documentation and also like context with a better, but like recommended by, by the, by the chief ingredients.
- 56:48Yeah.
- 56:49So like, no, not like, because I see like, yeah, some, sometimes like you, even on stack overflow, you see some answer, but you technically not, not agree the way it's done, but because it's like on the top and all LLMs already picking it up.
- 57:03And it's, it's getting like this kind of boost that you don't really want to.
- 57:08And this is like, I think it becomes kind of problematic that, you know, that you cannot really suggest to large language models.
- 57:14Okay.
- 57:15What's the recommended way and why it's better than, than it's currently like suggested.
- 57:19So yeah, this is, I, yeah, but this is like more like a, it's, it's a new problem that never existed before, but you have to rely on it.
- 57:26So concurrently, I guess the best way is like to stick with your API forever and just try to, to do stuff on the, under the hood.
- 57:34So that, you know, users, users always use the, how it was designed at the beginning and yeah, like never, never feel any differences.
- 57:42Easier said than done.
- 57:43I will add though, that your documentation has really nice search.
- 57:46So that that's easy to go here and, and even without the LLM it's pretty approachable.
- 57:53So it's not, you know, I don't feel like you have to use LLMs to figure this out.
- 57:58I'm sure people will.
- 57:59That's actually also the goal, like how, how you make it so that people don't, don't, don't ask any question.
- 58:04Don't Google like it should be like intuitive.
- 58:06Let's wrap it up with a thought, I guess, but we talked about using Python typing as a core element of the framework.
- 58:13And I don't know about other people, but for me, the fact that it has strong typing and maybe put a little bit of that into your code as well.
- 58:21I find I go to the documentation much less when I do that, or I'm using APIs like that because you just, your editor just tells you, well, here's the three things.
- 58:29You go, well, this must be the one.
- 58:30And you know, it guides you down the path.
- 58:33But if the editor is not there to help, because it doesn't have the type information, then, then you have to go to the docs or you have to go to search and it's just, it's not great.
- 58:41Yeah.
- 58:41So I think it's very approachable.
- 58:42Even if you use some, some stuff like a pilot.
- 58:44Yeah.
- 58:45So even if suggested something wrong, but if you like have types that and your editor immediately like highlighted you with some kind of red, red, wavy thing that it's you don't have this attribute and you already like sold it.
- 58:56And yeah, it's makes you laugh easier.
- 58:58Yeah, it absolutely does.
- 58:59All right.
- 59:00Final thoughts.
- 59:01People wouldn't get started with Django Ninja.
- 59:03What do you tell them?
- 59:04It's definitely worth trying if you like never, because what I often see is like on Reddit on like any, any resources, like people ask, okay, so I have JNDRF and Django Ninja.
- 59:15So what should I try?
- 59:16And you're like, there's always like people who answer a bunch of stuff and pros and cons.
- 59:22Like, yeah, like I think at this point, like, yeah, but before there was like big argument because the Django rest framework is really old stalled product that is like, you know, have like large community.
- 59:33I think at this point, Django Ninja also becomes this old thing as well, because like, it's already like, I think it's like five, it's five years will be like somewhere this year or more, even more.
- 59:44I don't count.
- 59:45It's already like have millions of downloads and it's all like, I know very big, big names of companies who use it and they use it in a pretty kind of sensitive areas.
- 59:56So yeah, should not, not get worried.
- 59:58And because also like, I guess one of the things that I'm trying to, as a strategy for this product, I try to be this as, you know, subtle as possible without like, you know, risky moves or changes.
- 1:00:11Try to make it for people to migrate to newer, newer versions easier.
- 1:00:15So like, I guess the hardest part was like, you know, between version zero something to 1.0, but yeah, it will, it was like dictated by the fact that, you know, Pydantic moved from one to two.
- 1:00:26So like, there was no way to make it without breaking changes.
- 1:00:30But my goal is like, like actually like, you know, even in this LLM era to try to introduce new features that should not force you to like, you know, may have a breaking changes year in your code.
- 1:00:42So yeah, that's my, that's my goal.
- 1:00:44And yeah, this is why you should try it.
- 1:00:46So if you, if you, if you like try it in introducing to your code, then you probably will be good, good for the, for the good amount of time.
- 1:00:54And I think like one more thing that we didn't talk about, but you should definitely try is, is project called nano Django.
- 1:01:01So this is a way for you to write a code, like a pro Django project, but just in one file.
- 1:01:08So under the hood, it also uses Django Ninja.
- 1:01:11So this is also like a very, very easy thing to be, to join the Django Ninja tryout thing.
- 1:01:18So here in one file, you can define your model.
- 1:01:20You can define your API, run it with one command and you're ready to go.
- 1:01:24And basically like same, same way you used to work with Flask or FastAPI.
- 1:01:29But the coolest thing about this is like when you see, okay, your, your file grow to like, you know, hundreds of thousands of lines.
- 1:01:38There is a tooling that you just, it can convert for you, like your single file into a standard Django project structure.
- 1:01:45And that, that way you can, you know, you start small, then you see you're growing and then it, it, it will convert you go to, you know, standard Django configuration,
- 1:01:54process and all the conventions.
- 1:01:56And yeah, you continue your project without, without any,
- 1:01:59Yeah.
- 1:02:00Yeah.
- 1:02:01Nano Django is an interesting project for sure.
- 1:02:03It's like, let's start simple, but if you need it to be more structured, then it'll actually explode out the file and stuff.
- 1:02:09This is like a very first way to try that to do.
- 1:02:11To just simply install Manage Jenga and you'll have Jenga in that hood, Ninja under the hood and everything you need all in one, in one file.
- 1:02:18Cool.
- 1:02:19Awesome.
- 1:02:20I'll definitely put that in the links for people as well.
- 1:02:21Well, Vitaly, thank you for being here.
- 1:02:23I did mention at the top, but I do want to send out some thoughts and support for the folks out in Ukraine as well.
- 1:02:29I know it's a ridiculous time and be safe out there.
- 1:02:33You guys.
- 1:02:34Thank you.
- 1:02:34Thank you for being here.
- 1:02:35Thank you for creating this awesome project and coming on the show and sharing it with us.
- 1:02:38Thank you for having me.
- 1:02:39Thank you very much.
- 1:02:40Yeah.
- 1:02:41Bye.
- 1:02:42Bye.
- 1:02:42This has been another episode of talk Python to me.
- 1:02:44Thank you to our sponsors.
- 1:02:46Be sure to check out what they're offering.
- 1:02:47It really helps support the show.
- 1:02:49Take some stress out of your life.
- 1:02:51Get notified immediately about errors and performance issues in your web or mobile applications with Sentry.
- 1:02:57Just visit talkpython.fm/sentry and get started for free.
- 1:03:02And be sure to use the promo code talkpython, all one word.
- 1:03:06And this episode is brought to you by Bluehost.
- 1:03:08Do you need a website fast?
- 1:03:10Get Bluehost.
- 1:03:11Their AI builds your WordPress site in minutes and their built-in tools optimize your growth.
- 1:03:16Don't wait.
- 1:03:17Visit talkpython.fm/bluehost to get started.
- 1:03:21Want to level up your Python?
- 1:03:22We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:03:26Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:03:31And best of all, there's not a subscription in sight.
- 1:03:33Check it out for yourself at training.talkpython.fm.
- 1:03:36Be sure to subscribe to the show.
- 1:03:39Open your favorite podcast app and search for Python.
- 1:03:41We should be right at the top.
- 1:03:43You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 1:03:52We're live streaming most of our recordings these days.
- 1:03:55If you want to be part of the show and have your comments featured on the air, be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:04:02This is your host, Michael Kennedy.
- 1:04:05Thanks so much for listening.
- 1:04:06I really appreciate it.
- 1:04:07Now get out there and write some Python code.
- 1:04:09write some Python code.
- 1:04:10Open arms!
- 1:04:13Open arms!
- 1:04:15Open arms!
- 1:04:17Open arms!
- 1:04:19Open arms!
- 1:04:21Open arms!
- 1:04:23Open arms!
- 1:04:25Open arms!
- 1:04:27you
- 1:04:28you
- 1:04:28Thank you.
- 1:04:29Thank you.