Python for the .NET developer Transcripts
Chapter: Database access and ORMs in Python
Lecture: Databases and Python
0:00 Databases are central to most applications
0:03 especially server side applications like websites
0:07 and Web APIs.
0:08 So when you think about working with a database
0:11 there's a really good chance that in the .NET world
0:14 your thinking Entity Framework and Microsoft SQL Server.
0:18 That's the most common way in which we work with databases.
0:22 Yeah you can do, just open up a connection
0:24 and do select * from table, et cetera.
0:27 That is direct SQL type queries. People do it but
0:32 and there's reasons you might want to do it
0:33 but, most of the time, you want to use an ORM
0:36 and in .NET the ORM most of the time is
0:39 Entity Framework.
0:40 We saw what happened when we had this one thing in .NET
0:44 and went over to Python in the web world
0:46 it exploded in to a huge number
0:49 of these different frameworks.
0:50 Sort of true in Python as well, but it actually turned out
0:52 to be a little more focused. In Python
0:55 we have two primary relational database access ORMs
1:00 for us. If you're using Django, there's a Django ORM.
1:04 It's very likely you're going to use that.
1:06 If you're not using Django, most of the time
1:09 people just assume you're using SQLAlchemy.
1:11 SQLAlchemy has been around for a really long time
1:14 it's super popular and it works great.
1:16 It's high performance.
1:17 We're going to talk about a lot of the ways
1:19 in which it works and that's the one we're going to choose.
1:23 If we jump back to our JetBrains PSF Survey from 2018
1:27 you can go and find the ORM section and see
1:30 well, it's a pretty clear split here.
1:32 If you're using Django
1:34 you're going to be using Django ORM.
1:35 Other than that, chances are you're using SQLAlchemy.
1:38 There are reasons to use other ones
1:40 like peewee is pretty nice and peewee has good async support.
1:44 If you want to use, you know
1:45 async in a way to with the database
1:46 and SQLAlchemy doesn't have that built in.
1:48 So there's reasons to choose others
1:49 but these are the primary two and the decision is
1:52 do I have Django? Yes.
1:54 Then I use Django ORM, otherwise, SQLAlchemy.
1:57 There's other things and other ways
1:58 to access databases worth throwing out right now
2:01 that, you know, for example, I said earlier
2:03 our site runs on MongoDB, and SQLAlchemy and Django
2:06 those are for relational databases.
2:07 They don't make sense. So we use Mongo Engine.
2:10 If you're using Redis
2:11 or other databases that were not relational
2:14 this might not be the choice you want to make
2:16 probably doesn't work with that database.
2:18 Assuming you want to use a relational database
2:21 here's what the landscape in Python looks like.