what's going on everybody welcome to
part two of the web development with
Django and Python knowledge transference
okay in this video we're gonna be
talking about is models we're just gonna
start getting into models and how we can
interact with them but pretty much
models with Django is where you're
getting almost all of the value from the
Django web framework is coming from
models and all basically the abstraction
from those models so this is another one
where I think youyou would you might
want to spend a little more time making
sure you understand it really well
before you move on to the next topic so
with that let's get into it
so again models are just the way that
your website or your app gets to
interact with the information stored
within your database so for example on
our web site we might want to store like
our well for sure you're gonna store
like users to your database you're gonna
store so like for a user you might you
might store like a user name the actual
person's name an email a password all
that kind of stuff and then in our case
two we would like to likely store our
actual tutorials to the database we will
get two users I promise but that's Annie
that's a more complex issue because you
got to do the registration login and
sessions and hashing passwords all kinds
of stuff in forms and stuff like that so
for now we're gonna try to keep it more
simple we're gonna focus first on just a
simpler tutorial model so tutorial model
what kind of aspects or attributes what
a tutorial have we've got like the
tutorial content so this is just you
know the actual body of the tutorial but
then you've got like maybe a tutorial
title and then the published dates maybe
a playlist like or a series that it's
part of and then maybe like a category
or something like that we probably won't
do all of those yet but eventually that
might be what you would use now in terms
of other like if you were just trying to
come up with some way to store these
things to your database you would really
want to spend a lot of time before you
build your actual database out you'd
want to spend a lot of time think
like what might I need in this database
cuz you're not gonna want to migrate a
database but but with Django really is
so simple that you don't need to think
about everything in advance your it's
not going to cost you anything really to
later decide oh I want to have this
column or I want to I want to have this
other attribute or I don't really need
this one anymore so I'm gonna get rid of
it that kind of stuff it just it's not a
big deal with Django so anyways let's
get into it so with models we are just
going to navigate into the app that were
curious about so in our case that's main
we've been working there I'm gonna close
these just to avoid clutter and we're
gonna open up models dot pie so in here
you've really only got this one import
and that's just import models and all
this is for is every model is going to
inherit from Django's you know base
model so what we're gonna do is we're
just going to define a new class and
we're gonna call this class tutorial and
like I said it in inherits from models
dot model and all that is all that means
inheritance if you want you can go to
Python parent net go to the intermediate
tutorials and you can learn more about
inheritance but basically we are
extending this model so models dot model
already has a bunch of different
attributes and things associated with it
and then we can just say the little
things that we want to change so like
what columns and stuff like that but
honestly if you don't know what this
means or you don't care to know it's not
that big of a deal you should just know
that you have to you have to pass that
but if you want to learn more
it's just inheritance and you can look
more into it so anyways let me zoom in a
little bit here okay so what we're gonna
do now is just basically specify the
columns that we want in our tutorials so
you know maybe you're gonna have a
tutorial title okay and that tutorial
title would be models dot car field and
we'll give this a max length of 200 what
I want to look grab for you guys is I'd
like to show you the documentation so
let me think your Django model fee
hopefully that will take us where we
want to be let me just do a quick search
for car fuel
looks like it's a probably on this page
yeah okay so um I will put a link to the
text-based version of this oil for sure
in the description otherwise you can
just do the same google search I just
did and find yourself here but obviously
this is quite a large page there are
many different fields that you can have
I'll explain the ones I'm going to use
but just know that many many more exist
and you should just come here to learn
about at least all of your options but
the ones were like most people are going
to use like car filled a text input or
something text field and then there's
maybe like a date input type and then
maybe like numbers only stuff like that
pretty much people are gonna use all the
same keys and you probably won't need 90
percent of the available keys
so anyways back to where we were so this
is a car filled max length is 200 then
we're gonna add a tutorial contents and
that will be models models dot text
field and then no parameters so then
immediately the astute among us would be
like well Bobo what's the difference
between a car field in a text field so a
car field or a care field or whatever
you want to call it char field I have
heard people call that a char field
anyways whatever you want to call it the
car field is basically if if you want to
set a maximum and generally car fields
gonna be a shorter thing whereas a text
field is like a blob although I bet I
would be surprised let's see if we
search blob oh really
text blob no okay
anyway I thought they would probably
have a unique one for blob but maybe
that's what it maps to an SQL or
something is just like a text blob or
something anyway text field is like what
you would use for a tutorial or an
article or something of not finite
length so anyways the next thing we're
gonna do tutorial content oh let's do
tutorial
ished must be models dot date/time field
and then we're gonna have this called
date published published okay so that's
enough information also we can oh we can
do various overrides like I was talking
about so we can override the string
methods so we could return tutorial
title and in fact first of all this
would need to be self self my eye is
super self so all we're doing
this for is because generally if we
display this object and like print out
the object let's say it's gonna be look
kind of ugly it's gonna be like
something like tutorial object blah blah
blah and instead what we should say is
as we iterate over it maybe we want to
know the title or something like that
like so it just it just makes it a
little better to read so okay so we've
got our model now anytime you create a
model basically a model is going to map
to a database table so this is our so
we've got our database already basically
alright you already should have a
database I'm trying to decide where it
would be probably back here yeah here's
your database right here DB sqlite3 the
default is SQLite later you can change
that if you go to my site settings dot
PI and then somewhere in here it'll be
somewhere in here anyways oh right here
probably yes so this will be your
database engine you could change it
SQLite is great for development and
tutorials like we're doing right now you
can scale an SQLite database actually
quite well so I wouldn't you know the
conventional internet wisdom will tell
you I never use SQLite in production
maybe SQLite it's actually pretty darn
powerful it's just that a MySQL or
something like that or even some of
these other databases is probably a
better choice in your deployment but
well we can were even talk about that
way later down the road
SQLite 3 is nice because it comes with
Python and nobody has to do installation
and everybody can follow along
no matter the operating system
we'll keep sqlite3 and anyways you
already have your table so what this is
going to do is or I'm sorry you already
have your database what this is going to
do is create a new table tutorial and
then all of these things become your
like your columns in that table and then
it automatically just by default gives
you a primary key so anybody who's
familiar with making databases and stuff
is like wait wait wait
you will already have a primary key it's
just default done for you but you could
also if you wanted set one of these to
be your primary key if you so chose but
we're not gonna focus on that for now so
every time you make a new model that's a
new table therefore you have to do two
things with Django one you have to do a
thing called make migrations and all
that does is kind of prepare your
migrations then you have to actually
migrate so it's two steps so as you've
seen already when we run our server I'm
assuming one is still running nope I
guess I'm not running a server anymore
so Python managed by run server it was
giving us a message before let's see
what it does this time okay so it says
you know you we need to probably run a
migrate this is really only like because
we just made this website that we want
to run this so what I'm gonna do I guess
CD what do we call this my site right my
site yes so I think it's in here so what
we can do is I just I hate that I'm just
gonna do it in order that you're
supposed to do it we'll do the other
migrations at the same time because
there's only one time you'll run that
migrate first so what we're gonna say
here is basically every time you add or
change so if you add another model or
you want to change a pre-existing model
you have to make migrations then migrate
and that's basically it unless you
happen to hit an error so we've made
this new model so the first thing I want
us to do is make migrations so we're
just gonna say python managed PI make
migrations and it says no changes
detected
so what
happened here the reason why I really
did do this on purpose
you're gonna hit this and if I did it
didn't hit it for you you would be
stumped or at least if you're like me
and you're slow you would be stumped for
a few minutes so what happens is anytime
you start a new app we were able to
point to that new app but we haven't
like installed that new app so first we
have to install that new app in order
for it to add tables to our database I'm
not really sure the design reason why we
can like have like URLs be pointed to it
and all that we could do that but we
can't do models I don't know
anyways that's the way it is and I guess
because if you you're explicitly
pointing but anyway I don't know it
feels funky to me but anytime you create
a new app you have to also install that
app if you want to use models so let's
go ahead and install it we're gonna come
over here to my site settings dot Piyush
it was already open and then it should
be near the top basically where we yeah
so we write right here this is where
you're gonna add any new apps that you
want to add to your project so let's
just go ahead and do it so in this case
it's actually main dot apps dot main
config that's what you need to add you
should immediately ask yourself does
main dot apps main config already exist
well we can actually check that really
quick main app spume here we are main
config name equals main great so it
already exists sounds good so as long as
settings is saved we'll come back over
here and let's try to make migrations
again sure enough we made a migration so
all this says is it just kind of gives
us a quick summary of what it's done for
us so it's just saying hey we're gonna
make a basically we're adding a new
model if you were gonna change things it
would say hey we're changing something
to something in blah blah blah and all
that also you can come into your
directory here and then now we've got
this migrations and you can kind of see
like what its plan is for the future if
you open that I don't know why I should
have stayed in sublime but anyways if
you want you can check that out you
probably will never check it out
but if you wanted to you sure could the
other thing you could do is actually get
the SQL for it so if you want to do this
you could say Python managed by SQL
migrate main and then you get the ID so
in this case it was 0 0 0 1 hit enter
here and this just tells you the exact
SQL should you be curious that will be
executed maybe if you like you know SQL
a lot you'll you just you just want to
see how they're doing it or something
but again you're probably never doing
that really all you're gonna do is you
make a new model you run the make
migrations that like Python manage up
high
make migrations we can run it again it's
just gonna say nothing new
then all you were gonna run is Python
managed by migrate so this time it did
quite a few things only because we just
never ran that migrate that it was
asking us to run I personally found that
to be kind of confusing that we
initially ran migrate and then later we
make a model and then you got a run make
migrations and then migrate again like I
just really stumped me all you need to
know to do that something with a model
make migrations migrate done it's not
complicated I promise so our model is
completed there now we're ready to start
interacting with it so the next thing
that I think is kind of cool that I
don't think you're gonna need it this
early in but I do think it's it's just a
cool aspect to Django that again if you
were doing things on your own you would
probably never make for yourself but
it's pretty cool that we have this so
one thing that we can say is Python
managed dot pie shell and this gives us
like an interactive shell to play with
our website so rather than like if
you're trying to do something like for
example let's say we make some tutorials
then we want to iterate over the
tutorial but something's going wrong and
especially like later on when we have
like foreign keys that are pointing to
different tables and we're trying to
figure out okay how can we pull and how
do we write the statement that we want
to write and if you're trying to like
build a view and a MA or yeah if you're
trying to do like views dot pie and then
put it to a template and then like
navigate to that
page and reload that page just to see if
it worked it's a very tedious so instead
you could use something like the shell
so just real quick we can do something
like this we can say from Maine from man
Maine models we want to import our
tutorial model then what we could say is
like tutorial.org X dot fall and this is
the exact same syntax that you would
actually use in like for example your
views dot pie
you wouldn't say from Maine it would be
a relative important but anyways you
would you would do something very
similar to this at least and actually
tutorial objects to all this is
something that you would do so because
you can do like dot all you can filter
you can get you can do all kinds of cool
stuff all of which I think all of which
will will end up actually using so
anyways in this case we see there's
nothing here where there are no tutorial
objects all we did was just create the
table of the database so we would need
to like add a tutorial in here so we can
definitely do that we have tutorial
title content and published so we real
quickly we can just add one in so we
could say we can create this like new
tutorial object we're gonna say that is
equal to a tutorial and then we'll say
tutorial title equals to B can anybody
guess we're just going to toriel content
equals or not to be
and finally oh we should have done
goofed I don't know if I can get away
with this because what we want to do is
actually use a date/time here I think
I'm gonna let it throw a syntaxerror and
then I'll just use the up arrow cuz I s
I could copy it too but I'm not really
worried about that then what we're gonna
say is a tutorial underscore published
equals and then I'm gonna say time zone
dot now this is gonna fail because we
don't have time zone but that's okay
cool so let's bring in time zone really
quick that's from Django dot utils
import time zone and then I'll just run
that again so now we've got this new
tutorial
object and again if you're familiar with
SQL you kind of you can build an
insertion but then at the end of the day
you've got to commit it so we're gonna
go ahead and new tutorial dot save
because commit is a horrible word and
nobody understands it doesn't make sense
to a newcomer anyways so I new tutorial
about save now we actually have it so
now if I just up arrow all the way
through objects to off boom we've got
one later we could iterate over that so
for T in tutorial dot on Jack's dot all
whoops dot all one two three four prints
T dot tutorial title for example whoops
did I let's see what did I do wrong
forty and tutorial T dot tutorial what
did I do wrong there I feel like I've
got to be like totally blind here 4000
capital T tutorial is my issue there I
was like why did I use tutorial ok print
T dots tutorial title did it okay great
so um with again this is the same sort
of syntax and logic that you would use
to actually iterate over your objects in
save you spy before you might pass it to
a template but also you could run this
in a template which probably means
nothing to you at the moment but later
it will anyways pretty cool that Django
basically we've abstracted quite a bit
away from writing our own SQL queries
and all that and later if we wanted to
change things and we will probably
change things it's like super simple to
go about changing things so I think at
this point I'm gonna leave it here and I
think in the next tutorial we'll talk
about is likely the admin page whenever
we did this we also brought in our admin
so we just need to create an admin user
account then I can show you guys the
admin page which is again one of the
reasons why your models are just
incredible
though that we have in the admin page
from Django is
just just exquisite so I'm really
excited to show you guys that's again
one of the coolest things that you get
from Django and yeah so questions
comments concerns whatever feel free to
leave those below quick shout out to my
most recent channel members Rodolfo CID
kg1 son and Ollie thank you guys very
much for your support I really
appreciate it lets me do this kind of
stuff and i truly love doing this
so that's it for now if you didn't know
how to become a channel member you click
that beautiful beautiful blue blue
button there that's it for now I will
see you guys in another video where I
continue showing you guys the magic of
Django