Hello again, in this video we are going to add
some codes into our models.py file.
As I explained to you, a page, a web page
has a model, a view and an HTML template.
So the model is lower in this trio structure.
Therefore, we are going to start with the models.
What we need to create is a
class, to represent a blog post.
So let's call this a Post.
Now, Post is not a simple class.
Post should be a class
which inherits from, models.Model.
So that makes this class a Model class.
So Model is a specific class which is designed
by the Django authors, the developers of Django, and
it's designed to contain fields of data.
[No Audio]
What do I mean by that?
Well, for example, we said that we want a
title field, a web page as a title.
So that title, when the content creator creates
a title, that title will be stored in
the title field, of the Post table.
So I showed you this DB viewer, which is a program
to see data, to view the data of an SQL database.
So the SQL database is this
one in here, db.sqlite3.
And this Post will create a new
table in the, dbsqlite3 database.
Now these are the tables currently.
So there's no Post table here, but you'll see that
one will be created when we complete this table.
So title now is models.,
what is a title?
Is it a CharField?
Yes, I think this type would fit a title.
So this is a database field type, and it
gets a max_length attribute,
[No Audio]
let's say 200 characters.
So there will be a text box, that will allow
up to 200 characters, for the title of the blog.
[No Audio]
Then what else do we have? content,
so the body of the blog.
Now here, I wouldn't go with CharField, because that
is designed more for things, for small pieces of text.
This would be more like a text field,
[No Audio]
and you can leave that empty.
What else?
Well, date_created maybe, which would
be models.
[Author Typing]
DateTimeField.
So you see that, this models module
of Django, has all these fields ready.
So a DateTimeField would be auto, auto_now_add=True,
which means that, when the content creator will be creating a
new blog post from the admin interface,
so they will be writing a title, a content, and then
there will be a save button there, where they can press
that save button and the Post will be live.
When they press, when the contents creator presses
that button, this class will be instantiated and
it will generate the current Date and Time.
And that current Date and Time will be
injected into the date_created field of the
Post table, in the db.sqlite3 database.
So the database has several tables,
Post is one of the tables, Post has several fields,
one of the fields is that.
Okay, I hope that makes sense.
Then we have the slug field, which will be
recording, the part of the URL after the domain.
So as we said, if your domain is example.com and your
Blog Post is about, is about dogs, then the slug will be /dogs.
So the complete URL would be, example.com/dogs.
And here the user, the contents creator will
just write dog or wherever they like.
So the contents creator will enter the slug
in the admin interface, just like they do
with title, content and other, input boxes there.
So slug would be models, again .Slugfield.
So there is a specific class that represents
SlugFields in Django, called SlugField.
Let's put a max_length of 200.
Maybe it makes sense, not to have very long slugs.
So this will not allow the content creator to enter slugs
longer than 200 characters.
And it has to be unique.
So unique equals to True.
If the admin, the content creator enters a slag
which has been entered before, that would not allow
the new slug to be created because, you cannot
have two pages with the same URL.
It would mess things up.
Therefore this argument doesn't let
duplicated URLs to be created.
Then we have the author.
So author the variable name
models.ForeignKey this time, why?
Because, we are going to get this value from another
database table, which has already been created by Django
and we can get that from,
django.contrib. auth.models import.
So from that import User, right.
This is a class also you can
see it, when I hover the mouse.
And therefore in the two arguments here
we need User,
[No Audio]
without the parentheses.
So basically a ForeignKey field type
means that, the value of this database
field, will come from another database table.
In this case is a User database table.
And this field will create on the admin
interface, a drop down list, where the content
creator can choose between different user, usernames.
So it happens automatically.
But we want to specify a few parameters on_delete,
the behavior that will happen when the user is deleted.
So this user is deleted
from the original database table.
[Author Typing]
So CASCADE, but you don't need this, so
delete that, and just leave it like that.
Now, what does this mean?
Well, this means that if a User is deleted from the
user database table, the User Posts will also be deleted.
This is what CASCADE does.
I'll delete this comma here, and the last field we
want to add is status, the status of the Post,
which would be equal to models.IntegerField.
On the admin interface, there will be a status dropdown
list, which will contain two options Draft and Publish.
So the content creator could choose to save
that article, either as Draft or as Published.
So this would make it easier for
content creators to organize blogs, because they
could save an article as drafts.
And then in our Django app, we could
make a conditional not to publish such articles
that have the status as Draft, but only
Publish those, who have the status as Published.
So this would be, a good way to
distinguish between the two types of Posts articles.
So we have to make to have choices which is equal to a
[No Audio]
variable, let's say STATUS.
And what is STATUS?
Well, STATUS has to be a
variable, we have to create.
Here, so status equal to a tuple
[No Audio]
of two tuples,
[No Audio]
one would be, let's say 0
Draft, the other one would be, 1
[Author Typing]
Publish, and the default one would be maybe Draft.
So 0o stands for Draft and 1 would be for Publish.
Now the admin, the content creator will see either
Draft or Publish in that drop down list.
And that completes our class Post.
Now make sure you save this file, File
Save or Command+S or CTRL+S.
And then you need to do something here.
We need to apply that model to our database.
So we have to run some SQL queries.
But as I have mentioned before, you
don't need to run SQL queries explicitly.
All you have to do is run
two commands python manage.py makemigrations.
So currently, if you have a look at the
DB Browser program where we see the data, we
don't have any tables called Posts for now, even
if I refresh this, refresh, there's no such tables.
But now see what's going to
happen, if I run this command.
Okay, something happens here.
Migrations were applied, so the
database structure was changed.
And then we need to add another command which
is that's python manage.py, migrate this time.
So that one and that one will
apply, SQL queries corresponding to this model.
Now, if I go to here refresh,
[No Audio]
you will see this table has been added blog_post.
So the name of this table was constructed using
the app name, which is blog and underscore.
So Django does that automatically and then after the
_post, which is the name of the model.
So if I select that table, you see now that,
we have these, database fields.
So here we see all the fields that we created,
id a field that is created automatically, but we have
title, content, date_created, slug, status, and author_id, right.
That completes the creation of our model.
Now we need to create a view.
Let's do that in the next video.