Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
What good is a database without tables? Let's build a model, connect to our database, and turn our model into a table.
New Terms
- model - A code object that represents a database table
-
SqliteDatabase
- The class from Peewee that lets us connect to an SQLite database -
Model
- The Peewee class that we extend to make a model -
CharField
- A Peewee field that holds onto characters. It's avarchar
in SQL terms -
max_length
- The maximum number of characters in aCharField
-
IntegerField
- A Peewee field that holds an integer -
default
- A default value for the field if one isn't provided -
unique
- Whether the value in the field can be repeated in the table -
.connect()
- A database method that connects to the database -
.create_tables()
- A database method to create the tables for the specified models. -
safe
- Whether or not to throw errors if the table(s) you're attempting to create already exist
-
0:00
Models in peewee are Python classes.
-
0:02
Each column in the table, each aspect that we want to remember about our thing.
-
0:06
Is an attribute on the Model Class.
-
0:08
To demonstrate this, let's make a model to represent a student here at Treehouse.
-
0:12
So, let's start by creating a new file that we're going to call, Students.py.
-
0:19
And I'm going to hold all of our information about
-
0:22
the different students in a dictionary.
-
0:24
But before I do that, I need to go ahead and make the database and
-
0:29
the model and all of that stuff.
-
0:31
You know, cart before the horse kind of thing.
-
0:35
So, to do most of this, I need to import the stuff that I need from peewee.
-
0:40
But unlike most of the platform that we've been writing,
-
0:43
we're going to be using a lot of different stuff in peewee.
-
0:46
And so, the convention for that is, to actually go ahead and
-
0:48
just import everything.
-
0:52
I don't really like doing this, it's not my favorite technique.
-
0:57
But this is the convention for working with peewee and
-
0:59
it's pretty important to stick to conventions.
-
1:01
Because that way your code, looks like other people's code.
-
1:05
Everybody can make assumptions and just work.
-
1:08
So we're going to import everything.
-
1:09
So now, let's make our database.
-
1:12
We're going to be using SQLite so we wanna make a SQLite connection.
-
1:17
Peewee will create the database for
-
1:18
us if it doesn't exist, at least when we're using SQLite.
-
1:21
So, Sqlitedatabase, and
-
1:25
I'm going to call it Students.db.
-
1:29
The .db is kind of a bit of a convention, because it says, hey, this is a database.
-
1:35
You don't have to, of course, if you don't want to.
-
1:38
And you can name the file anything you want.
-
1:39
If you don't want to call it students, if you want to call it people, whatever.
-
1:44
Go for it.
-
1:45
But now, we need our model.
-
1:48
Models in peewee are classes, but they inherit from the model class.
-
1:53
So let's do, class Student and it inherits from Model.
-
1:58
And we always name our models with a singular name.
-
2:03
Notice how I'm using Student and not Students.
-
2:06
So, why it that?
-
2:07
Well, it's because a model represents a single
-
2:09
item in the database, theoretically.
-
2:12
It wouldn't make sense then, to give them a plural name.
-
2:15
You'll see this convention used all over the place with peewee.
-
2:18
And you'll also see this used everywhere in Django.
-
2:22
Now, our model needs some attributes.
-
2:24
These attributes will become the fields or columns in the database table.
-
2:28
Let's just hold onto two things.
-
2:29
We're going to hold onto the student's username.
-
2:33
CharField.
-
2:40
And let's hold onto their points.
-
2:45
So, what's up with these?
-
2:49
Well, a CharField is a field that holds characters.
-
2:53
Somebody will say, CareField but I always find that really hard to say.
-
2:56
In sequel terminology, this is a VarChar field.
-
3:00
Because it has a variable length, VarChar variable character.
-
3:05
We have to tell it the maximum number of characters that we want to store.
-
3:09
So we're gonna set this to 255.
-
3:10
That should hold pretty much any username that we're going to ever come across.
-
3:16
The unique attribute,
-
3:17
the second argument there, says that we want the usernames to be unique.
-
3:23
We don't wanna have multiple records for the same student.
-
3:26
Our points attribute which is an integer field.
-
3:29
You probably guessed, integer fields hold integers.
-
3:33
And what we've said with this default keyword argument here,
-
3:37
is that if we don't supply a points.
-
3:40
Then it should just default it to zero.
-
3:43
Now, we didn't set a default on the user name field.
-
3:46
So, if we don't provide a user name we're going to get an error.
-
3:49
The last thing that we have to do is,
-
3:50
we have to tell the model what database it belongs to.
-
3:54
And we do that, by defining a class inside of our class that's called Meta.
-
4:00
Yeah, you can have classes inside of classes.
-
4:03
Now, metaclasses are something you're going to hear about
-
4:06
a lot of different places.
-
4:07
And they're a huge part of programming, but this isn't a metaclass.
-
4:11
This is just a class that's called meta.
-
4:13
It's describing the class that it belongs to.
-
4:15
The name has some meaning, but
-
4:17
it doesn't necessarily mean the thing that the name reflects on.
-
4:21
I know that's a little weird, but again,
-
4:23
this is a convention you're going to see in peewee and in Django.
-
4:27
So, we can use the metaclass to define all sorts of things.
-
4:31
Like which fields should be indexed,
-
4:33
how things should be ordered by default, all that stuff.
-
4:36
But for right now,
-
4:37
we're just going to stick to one attribute which is the database attribute.
-
4:40
And we're going to set database as being equal to DB.
-
4:44
which is our SQLite database we created up on line three.
-
4:49
Okay so, we have a database and we have a model that's been described.
-
4:54
We should use them.
-
4:55
So, let's add some code in here.
-
4:59
That if our file is run directly and not imported.
-
5:06
That it will go ahead and connect to our database.
-
5:10
And it will create tables and the tables we want it to create is, or are student.
-
5:17
We only have one table.
-
5:19
But something we want to do is, going to be sure and say safe equals true.
-
5:23
The reason we have to pass in the safe equals true keyword argument.
-
5:29
Is so, that if we run this multiple times toward the database that's
-
5:32
already been created.
-
5:32
And the table has already been created, peewee won't freak out and break.
-
5:37
It'll just go, okay, cool, everything's there.
-
5:40
Let's move on.
-
5:42
Okay, let's try and run this.
-
5:46
We come down here, python students.py.
-
5:49
And then I should be able to run this a second time with no problems.
-
5:55
Yay!
-
5:55
All right, so let's do an ls, and there's our students.db.
-
5:59
We're not going to talk about everything in SQL lite.
-
6:02
But let's use SQL lite real quick, to look inside there and see what's going on.
-
6:06
So, we can do SQL lite 3, students.db.
-
6:10
That'll connect to our database.
-
6:12
We can do .tables and that will show us all of our tables.
-
6:17
So, we have a student table.
-
6:19
And we can do say ,select * from student.
-
6:24
And we get back nothing, because we haven't put anything in there.
-
6:27
And then we can do .exit, and that'll go ahead and leave SQL lite.
-
6:31
Great, we're now ready to go build something.
-
6:35
Connecting to a database and creating a model are both pretty simple.
-
6:38
Most of that is thanks to peewee's API.
-
6:40
Now that we have a table, let's insert some data into it and
-
6:43
see about getting that data back out.
You need to sign up for Treehouse in order to download course files.
Sign up