#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: Create a Django model and migrate it to the DB
0:01 When working with a database in Django
0:03 we use Django's ORM.
0:06 It's a way to abstract the SQL away from us
0:09 so that we can write Python to work with a database.
0:13 Here I define a Quote class or model
0:17 which inherits from models.Model
0:20 in a specified couple of fields
0:22 which will end up being the database columns.
0:25 There are various types
0:26 so TextField, CharField, URLField
0:28 DateTimeField, and these will translate
0:31 in various properties of the columns.
0:33 So, the TextField will be variable length.
0:36 CharField in this case will have a max length of 100.
0:40 URLField will give us validation
0:42 out of the box to only enter valid URLs.
0:45 And DateTimeFields, convert the data
0:48 into proper DateTime objects.
0:50 The way in Django to make a field optional
0:53 is to use null=True
0:55 so the source and the cover fields are optional.
0:58 blank=True are used
0:59 to also make it optional when used in a form.
1:02 The difference between added and edited is that added
1:06 I want to add the DateTime up on adding the record.
1:09 And, I'm using auto_now_add for that.
1:12 Edited though, I want to be updated
1:14 every time a change is made to a record.
1:17 And to do that is to use auto_now.
1:20 So, auto_now updates with the DateTime
1:22 every time a record is edited.
1:24 Added does that only the first time.
1:27 By looking at quotes in the app and backend later
1:30 or in the Django shell, there's not that much
1:32 information displayed by default.
1:34 You just would see Object.
1:36 And to have the object show more meaningful information
1:39 I defined __str__ and gave it self quote and self author
1:44 and lastly the metaclass is just a way to
1:48 define more properties of the model and
1:51 in this case I just define the ordering attribute
1:54 to sort the quotes by when they were added.
1:57 So the most recent quote will appear at the top.
2:00 Back at the app, in the quotes
2:04 there's a models.py file
2:08 and I'm going to add the code here.
2:14 The next step is to make a migration file
2:17 before Django can sync anything to the database
2:21 it needs to make a conversion from Python to SQL.
2:25 So I'm going to use manage.py
2:30 with the flag makemigrations
2:36 you see the file that Django created
2:39 this is something you want to include
2:41 in your version control
2:43 because this that you can replay
2:47 the various modifications to the database.
2:51 So here you see the intermediate code
2:54 that Django created and when we now type migrate
2:58 Django is going to use this to create the table
3:01 in the database.
3:11 So apply the migration file
3:14 and now I can open a database.
3:28 And I'm using DB Browser for SQLite.
3:34 Again the database file is in the root
3:36 directory of the project. I can drag it over.
3:44 So here we see what Django created.
3:47 The table name is always the app name underscore
3:51 model name lowercased.
3:54 You have a primary key of id
3:56 we didn't have to specify it. Django does that
3:58 for us.
4:00 We have the TextField
4:01 we have the author CharField we limited to 100
4:05 sort and cover which are URLFields
4:08 and Django limits URLFields to 200 characters
4:11 and we have the added and edited as DateTimes.
4:14 Next I'm going to use the Django shell
4:16 to add, edit, and delete objects with Django's ORM.