Now, I believe the best way to learn is by doing so in this section to learn how to create an API with
Django.
We are going to be creating our own custom version of Reddit.
Basically a simple version of Reddit.
Now, if you don't know what Reddit is, Reddit dot com, it's basically a Web site where people can
post links and then people can up or down vote those links.
They can also comment on those links.
Right.
So you can see here at red dot com, there's a bunch of posts and this is the score of how many upvotes
they have.
And if we go into the comments, like it's going to show, you know, what people are saying about something
right now for us to build something this complex, there's all these comments and things like that,
it's probably a little bit out of our reach.
But we can create a service that is going to allow people to submit links and be able to upvote them
pretty quickly here using Django and making an API for it.
So just so you know what it is that we're going for, that's the general aim of this course now, because
this is a zappy code course.
We're going to instead of calling it Reddit, we're gonna call it zap it to kind of see how that works
there.
OK.
So let's go ahead and start a new Django project and open up my terminal again.
If you're on Windows, it's gonna be the command prompt.
And I assume you have some skills if you've worked with Django before moving around there.
Remember, the basics are if you want to move around, c.D, and then the name of the folder that you
want to go to.
And if you do a c.D dot dot, that sort of sends you back one.
There's a few differences between the command prompt and the terminal.
But again, if you need any help with that, Google that or take my intro to Django course, my Django
three course.
That'll get you up to speed.
All right.
So with those caveats, let's go ahead and move to the desktop.
And I'm going to start a new project by saying Django Dash Admen Start Project and I'm going to call
mine again.
Zap it.
Feel free to call your Reddit clone, whatever he want, but I'm going to go ahead and hit enter there
and look.
I have this new folder called Zap.
It's going to move into there and I say c.D.
And then zap it.
So once I'm inside here, I want to start a new app inside of here that's going to contain all the posts
that people create.
So that's going to be sort of our name for when people submit a link.
And people can up or down vote it.
It's going to be called a post.
So I'm going to go ahead and say here that I want to do Python three managed PI Start app and I'm going
to name this posts.
So for you, depending on what install that you have, you may be typing Python three or you may be
may just be typing Python managed up pie.
Again, if you've worked with Django, you probably have some experience there.
But now that we've done this, we should have this folder.
Zap it.
And inside of it, another folder called Zepa that contains like our settings, the posts that the settings
all there.
And then also this app that we just created called Posts, which has all the normal stuff like views
and whatnot in models.
This is where we're gonna make the model for our posts.
That should all look feel fairly familiar.
The one thing that I'm going to do is I always like to name the top level folders, something a little
different.
So that's different from this folder that I don't get confused.
I'm going to call the SAP It project so I can quickly see, OK.
This is the top level folder and then this is the one that has the settings and all those different
pieces.
OK.
So now that that's all created, I can open up my text editor, which I'm gonna be using.
Adam, feel free to use whatever text editor you'd like for this BLENKIN.
Go ahead and open up what I've got on my desktop.
OK.
So I'm going to go ahead and open up these Zap It project.
Awesome.
OK.
And now that I'm here, we need to start building out what this service is going to look like.
So notice we're not even going to be touching any of the API stuff yet.
First thing that we've got to cover is the model.
How do we represent when someone wants to create a post like what really goes into a poster?
Let's go ahead and start working here.
We're gonna make a new class called Post and this is going to use the models DOT capital model.
Same thing that was imported up here.
So what goes into a post?
Well, it's got to have a title.
You know, people are going to have a you are réal for the post, but there should be a title, as you
saw on the Reddit Web site.
Like they may be saying, hey, check out this article that did it.
Or look at this example of blah, blah, blah.
And the You URL is different from the title.
So we have a title.
And this is going to be using our models.
And it's going to be using a we're going to use a char field for this.
So I don't want it to be too long.
And I gonna say max length is equal to a hundred.
This is just sort of an arbitrary choice by me.
Feel free to do whatever you'd like there.
A post has to have you or else.
Let's go ahead and add that to something to say you are role is equal to models, Dot.
And we're gonna use the u r l field.
OK.
We don't need to pass anything special in there.
Then we need to know who it is that posted this.
It'd be nice to be able display hey, this was posted by, you know, Nick or Joe, Sara, whoever it
is.
So let's go ahead and add another field here called Poster K.
U arguably could call this user, but I like sort of the name poster.
It makes you remember.
OK.
Who is the person who posted this?
So that's why I called it poster.
And this is gonna be a model's dot.
And we need this to be a foreign key to foreign key.
And what we're looking for is the class user.
So we're gonna have to import this into our project.
So I'm going to say from Django dot com Trib.
Dot off dot models.
Yes, I know this is a long one here.
Models, import capital user.
So now that we have that, we can go ahead and put this in here.
Capital user.
And then we have to specify, hey, if this is, you know, ever deleted, how do we want something
that's referencing this post to happen?
So we're gonna choose the on underscore delete to be equal to models, DOT Kaskade, the Cascade Delete
gang.
And the last bit that we need here is we just need to know when it is that this was created, when did
someone make this post that'll kind of show some cool history on our Web site.
So we're going to say created is equal to models, dot, date, time, field.
OK.
And we're gonna specify here that this is just set whatever the time is, whenever someone got to make
sure I spelled out right date time field.
We're just going to set it to be whatever the current time is when someone creates this or going to
say auto underscore now underscore, ad and set that equal to true.
OK.
So now that we have this in place, this is what the post is going to look like.
It's got these four attributes.
And really, I think the most important thing is that whenever we see this, we should always have the
most recent things.
First, we're going to say class capital Medda.
And I'm going to specify here that the ordering on this and we're gonna just do an array here is going
to be a string.
And then just do negative created that says, you know, list things by that early state that they were
created.
So you're going to say, when does this come into play here, this ordering?
Well, when we list things out in the API and when we look on the admin portion of the Web site just
to see what's inside of our database.
This will help list things so that we have the most current things first.
So that's all that's contained in the Post.
You've worked with Django before.
There shouldn't be anything too shocking here.
The next thing is, you know, how do people vote on something?
Well, we could just go ahead and have like a vote count, like and we could, you know, make this
maybe like an integer field.
But we want to make sure that someone can't vote for the same thing a million times.
So if we're going to prevent that, we have to have a new class called vote that basically contains
a reference to a user and he posts.
So whenever once someone wants to vote on a post up vote post, we're gonna create a new vote object
that says who the person who voted was and also what they voted for.
And we can quickly count those whenever we're trying to get the sort of, quote unquote, score for
a post.
So there's going to be our second and last model.
But we're going to say class vote is going to be inherent in four models, that Model K or door in there.
And then we're going to specify that this has a voter, which the voter is essentially a user.
So I'm going to copy almost exactly what we have up here.
OK.
Space this down.
So the voter is a user.
And then we need to know which post it is that they're voting for.
So this is going to be pretty similar.
It's still a foreign key, but instead of user, this is going to be post.
OK.
So there you have it.
This is what the data model for our Zap It site is going to look like.
So.
So let's go ahead and save this.
I'm going to move back to the terminal.
And now that we've made changes to the models file, we need to make some migration's.
So I'm gonna to say Python three managed up high.
And I'm going to make migration's.
OK.
And it says no changes detected.
I forgot that inside of our settings.
We have to reference ah app that we created called Post.
I come down to the installed apps and I'm going to add posts that make sure I had the trailing comma
there.
I'm going to save this.
Now I'm going to run that same command.
I'm going to hit the up arrow to do that and hit enter and look.
Now it says, all right, here's the migration for your models.
And then we want to then migrate that.
So I'm going to change this to Python three managed up high migrate.
OK.
So there you can see it goes.
Updates the database for us.
So it's all ready to rock and roll.
This was a very big first step for getting our project started.
But we now have an awesome foundation and we can start building this thing.