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