Django: Getting Started Transcripts
Chapter: Django ORM and Databases
Lecture: Creating the Author model

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let me just open up an execution window here and I'm going to create a new Django app called catalog. This is where I'm going to put all our author and
0:14 book related stuff. You'll notice that the new folder has shown up in pie charm
0:20 Now I'll add the newly created app to the installed apps listing by editing the file.
0:32 With that done, I'm going to create a model inside of catalog. Now, I'll create my first model class one that contains information about an author.
0:48 Each Django model needs to inherit from the models.model class. I'm trying to think of a way of saying Model one more time in that sentence
0:56 The base class defines the query manager. Some default methods like saving to the database and is also a declaration to Django that
1:05 you want this to be a Model. Models have fields that contain data, each of which maps to a column in the database.
1:13 Let's start by adding a field to track the author's last name.
1:18 This line tells Django that you want a character field column in the database named last_name. Sequel has two ways of declaring string like things.
1:29 The first is a CharField which must have a length. The second is a less efficient field called text which doesn't have a length.
1:37 Unless you're storing arbitrary text blobs, you should prefer the CharField. The max length argument to the field specifies how many characters
1:46 the database should reserve for storing this data. Alexa had a first name.
1:55 This is pretty similar. The only new thing here is the blank equals true option There are two different kinds of empty.
2:02 For a text field in a database you can have an empty string or you can have null. Both the last name and first name fields are using an empty string.
2:13 Unless you have a very specific need otherwise, this is how you should specify your empty text in a database. So then what's this blank thing?
2:22 The blank attribute tells Django that when validating this field it is allowed to have no content.
2:27 Note that this is a Django level thing rather than a database level thing. In the future, when you build a web form that uses a data model underneath
2:37 or you use the Django admin to fill in a similar kind of form. Django uses this blank indicator to allow the first name field to be empty.
2:47 As the blank parameter defaults to false the last name field will be listed as required in any kind of corresponding form.
2:56 If you try to save an author object that violates these conditions, you'll get an exception. Okay, how about some numeric data?
3:06 There are a whole bunch of different kinds of number fields and quite honestly, this is probably not the best choice for storing a year,
3:13 but it's good enough for now.

Talk Python's Mastodon Michael Kennedy's Mastodon