Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We introduced model inheritance in Django Forms. Let's go back over that, review our other models, and update our Course model for one handy new feature.
You can create fixture data with python manage.py dumpdata
and then optionally provide an app or model name. You can load that data with python manage.py loaddata
and then providing the name of the fixture file. Django's documentation on fixtures is well worth a read.
Our models and
0:00
their corresponding database tables are
the backbone of any queries that we make.
0:01
They're what the ORM actually uses to
get all the information that we ask for.
0:05
Over the past few Django courses, we've
created and modified just a few models.
0:09
In this course, we're not going
to introduce any new models, but
0:13
we do need to make a change or
two to our course model.
0:15
Let's hop into Workspaces to
see how our models are and
0:18
what changes we're going to make.
0:21
Okay, before we get to the meat of this
course, I need to do a few things.
0:24
We need to add a new field to our course
model, so that we can tie it to a teacher.
0:28
I wanna just use the user
model that Django gave us,
0:31
there's no reason to reinvent
that particular wheel just yet.
0:35
So let's go to our models
inside of courses.
0:39
All right, so
first we need to import the user model,
0:44
Django has a module named contrib.
0:48
So from django.contrib and
inside contrib, there is a lot of useful,
0:50
but not required features.
0:55
It's where they all live,
auth is one of those features.
0:56
So our import is from
django.contrib.auth.models import User.
1:00
It's a pretty handy import to memorize,
1:06
there's a good chance that you'll
use it in most of your projects.
1:07
Okay, so now we need to add
the foreign key to our model.
1:11
So right down here, we're gonna
do teacher = models.ForeignKey,
1:17
and that's going to go to User.
1:22
All righty, so since we've added something
to our model, we need to do a migration.
1:24
So let cd into learning_site,
1:31
and then manage.py makemigrations courses.
1:34
And so you can see here that we're adding
those non-nullable field teacher to
1:39
the course without a default, and we can't
do that, we had to provide something.
1:43
So I know I have one user in here,
which is me, and I'm the teacher for
1:47
all the courses that are currently in
there, so I'm just gonna do one, and
1:51
I'm gonna enter in 1.
1:55
Now before we run this migration though,
I wanna change one more thing.
1:57
I wanna add a character field
into the course model, and
2:02
this is gonna hold on to what
the subject of the course is.
2:06
So let's just say subject
= models.Charfield,
2:10
default is a blank string,
and the max length is 100.
2:15
Now there's a really good chance,
like 99 point and
2:20
then as many 9s as you wanna type,
that if I was doing this for
2:23
an actual commercial product or
an actual project I was building,
2:26
I would have another model here
that represents the subject.
2:30
Or I'd have some sort of taxonomy system,
so
2:34
I could have multiple subjects, whatever.
2:36
For what we're doing here though,
both of those are a little bit overkill.
2:38
And so I'm not gonna do them, but
you should definitely feel free to try and
2:41
do those changes yourself.
2:45
So I wanna forcefy the default
because just like with the teacher,
2:47
I have to have a value to
put into our existing ones.
2:51
And yeah, I think we're good.
2:53
Let's go ahead and
make one more migration.
2:56
And then let's run our migrations,
3:00
manage.py migrate courses.
3:04
All right, so
our migrations have been made, and
3:08
we're ready to assign courses
the teachers and subjects.
3:12
I don't wanna do all that
on camera though, and
3:16
I don't wanna make you do it yourself.
3:18
So I've actually included a json
file right here, fixtures.json,
3:20
that has all of the data already in it.
3:25
It's called a fixture file because it has
fixed data that's going to get imported,
3:27
and so
I wanna just show you how to use this.
3:32
I mean, it's full of json data, and
3:35
every item in here relates
to a single database row.
3:37
There's a fields key here, and inside of
each of these is the column and the value.
3:40
And then at the end of that where,
there it is, model,
3:47
we have the app and
model that it belongs to.
3:51
Django will read all of this,
and it can create objects for
3:54
us based on what's inside of here.
3:57
It's a good way to set up starter data,
that's really about it though.
3:59
You don't wanna use this for
migrating from one database to another,
4:02
unless you're really, really careful.
4:06
All right, so let's go ahead and
4:08
load this with the appropriately
named loaddata command.
4:10
Loaddata, and it's fixtures.json.
4:16
Cool, so it installed 89
objects from our one fixture.
4:21
Wow, that was a pretty quick
review of our models, and
4:26
that fixture data is there waiting for
you if you want to use it.
4:29
The database in the workspace should
already have the data loaded though.
4:32
If you wanna make your own fixtures, you
can use the dump data management command.
4:36
I'll put a link to docs about
it in the teacher's notes.
4:39
Take a little break,
and when you come back,
4:42
we'll talk about a really handy tool for
evaluating your Django code.
4:44
You need to sign up for Treehouse in order to download course files.
Sign up