Django: Getting Started Transcripts
Chapter: Django ORM and Databases
Lecture: Introducing Django's ORM

Login or purchase this course to watch this video and the rest of the course contents.
0:00 You've seen how to get a site going and you've rendered some HTML. That's all you'll need for a static style website.
0:07 But what if you need to manage data? In this chapter, you'll be adding books to the database and then running queries to get that information back out.
0:14 Django comes with a database abstraction layer that lets you use objects to manipulate the rows in a database. In this chapter,
0:22 you'll be adding data to a SQL lite database through the use of model abstractions provided
0:29 by Django if you've ever used the most excellent SQLAlchemy Library. Django has a similar mechanism built in.
0:38 Models abstract away the database and changes to those models mean changes to the underlying data structures
0:43 Django provides tools for managing these kinds of changes through a migration tool set.
0:50 And of course you're going to want to put data into your database.
0:54 So, Django also has some management commands for importing and exporting your underlying data. These chunks of data are called fixtures.
1:03 What's computing without more acronyms? Well, O R M. That's object relational model even that's not quite helpful.
1:12 It means modeling or abstracting data through object oriented concepts. Essentially, you get to use all that fancy class stuff.
1:20 You learn to map tables into a database. The beauty of it is you don't have to think too much about the database.
1:27 The corresponding beast, is that more complex queries tend to be inefficient unless you truly
1:33 understand how Django is mapping your object relations to the database.
1:37 I've seen some truly scary code out there where the developer didn't understand what they were
1:41 asking the database to do but concentrated just on the class relations. Nothing in this course we'll get that messy but be aware that it can be a
1:49 problem at scale. Out of the box, Django supports five different databases. For this course, I'll be showing you SQL light.
1:58 This is a lightweight database that stores everything in a local file. Doesn't require a server and ships with Django.
2:05 So you'll have nothing else to install. If your favorite DB isn't here, you may be able to find a third party back end which you can use instead
2:13 In fact, there are some out there for no SQL implementations if you prefer. As your code evolves over time,
2:21 the underlying database will have to reflect any changes in your models. This typically is things like adding and removing columns.
2:29 Django comes with tools for managing this, which are mostly automatic, at least for the simpler cases.
2:36 Django also provides two management commands from importing and exporting data to and from your database.
2:42 It offers a couple of different serialization formats for your storage as well. The resulting files are called fixtures
2:47 and they're particularly helpful for setting up test databases For your test suites a model in Django is created by defining a Python class
2:57 objects based on the class correspond to values in the database or are about to be in the database.
3:03 Attributes in the class declaration to find the column types in the database. These attributes in Django parlance are called fields.
3:12 There are over 25 different types of fields and most of them work with all the supported databases. When you define a field,
3:19 you give it parameters which change how the field behaves. This allows you to express default values,
3:24 minimum and maximum values, the length of the field, and more. The data fields correspond to the kinds of columns you typically see in a database which
3:33 for the most part mapped to the kinds of data you see in Python, integers, characters, dates and more.
3:39 You can also define relationships between the objects. In a short while you'll be creating an author object that is related to one or
3:47 more book objects. Django supports three different main types of relationships. 1-1, 1 to many like the author in the books, and many to many.
3:57 There are also more complex ways of creating generic relationships that are beyond the scope of this course. Inside of each Django app,
4:04 there is a models.py file where Django looks for the definitions of model objects Once you've written a model,
4:11 you use a management command to map that into the database. This is called the migration process. The migration process has two steps.
4:20 The first creates a special Python script that defines the actual underlying sequel table,
4:25 creation statements and the second runs all the scripts that are new since the last time a migration was done. With your tables in place,
4:34 you want to put some data in them or get data out of them. Each class automatically gets a query manager that is responsible for this.
4:43 It is the interface to the underlying sequel, select update and delete statements. You can use the query manager to formulate parameter arised queries.
4:51 These return an iterable query result object that contains an instance of your class for each row in the database that matched your query.
5:00 For example, you could ask for all the authors that have a last name that
5:04 begins with T and get back a query result object with zero or more author objects inside. Okay, enough chit chat. Let's write some code.


Talk Python's Mastodon Michael Kennedy's Mastodon