Placeholder Image

字幕列表 影片播放

  • 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 jangle Web framework is coming from models and all basically the abstraction from those models.

  • So, um, this is another one where I think you you 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 air just the way that your website or your AP gets to interact with the information stored within your database.

  • So, for example, on our website, we might want a store like our Well, for sure, you're gonna start users to your database you're going to store.

  • Um, so, like, for a user, you might you might score like a user name thing, actual person's name and email password, all that kind of stuff, and then, in our case, too.

  • We would like to likely store our actual tutorials to the database.

  • We will get to users, I promise.

  • But that's a that's a more complex issue because you got to do the registration log in and sessions and hashing passwords, all kinds of stuff and 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 a tutorial model?

  • What kind of aspects are attributes with a tutorial?

  • Have we've got, like, the editorial 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 Kara Siri's that it's a part of and then maybe like a category or or something like that.

  • We were probably want to all 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 thinking ahead.

  • Like, what might I need in this database?

  • Because 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 advanced.

  • Uh, it's not gonna cost you anything, really Toe later.

  • Decide.

  • I want to have this column.

  • I wanna I wanna have this other attributes.

  • 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're just going to navigate into the app that we're 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, not 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 Jang Go's your base model.

  • So, uh, what we're gonna do is we're just gonna 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 el.

  • That means inheritance.

  • If you want, you can go to Python Parliament that go to the intermediate tutorials.

  • You can learn more about inheritance, but basically we are extending this model.

  • So models, that 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.

  • Um, but honestly, if you don't know what this means that 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, uh, 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 tutorial.

  • 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.

  • Ah, What?

  • I want to look crab for you guys is, uh, like to show you the documentation.

  • So me think you're Django Model Fields?

  • Hopefully that will take us where we want to be.

  • Let me just do a quick search for car field.

  • Looks like it's a pride on this page.

  • Yeah.

  • Okay, so I will put a link to the text based version of the Jewel, for sure in the description.

  • Otherwise, you could 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 toe, learn about at least all of your options, but the ones were like most people are gonna use, like our field a text employed 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 your pride won't need 90% of the available keys.

  • So, uh, anyways, back to where we were so This is a car field.

  • Max length is 200.

  • Then we're gonna add tutorial contents and that will be models, models dont's text field and then no perimeter.

  • So then immediately the astute among us would be like, Well, Bo, what's the difference between a car field in a text field?

  • So a car, field or care field or whatever you wanna call it Char field.

  • I I have heard people call that a tri field.

  • Anyways, whatever you wanna call it, the car field is basically if if you want to set a maximum, and generally car feels gonna be a shorter thing, whereas a text field is like a blob.

  • Although I bet it would be surprised.

  • Let's see if we search Blob.

  • Oh, really?

  • Text block.

  • 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.

  • That's just like a text blob or something.

  • Anyway, Text field is like what you would use for a tutorial or an article or something off.

  • Uh, not finite length.

  • So anyways, uh, next thing we're gonna do tutorial content, I'll Let's do tutorial published.

  • Let's be models don't date Time field.

  • And then we're gonna have this called date published published.

  • Okay, so that's enough information.

  • Also, we can we can do various overrides like I was talking about so we can override the string methods so we could return, um, tutorial title.

  • And in fact, first of all, this would need to be self self, My eyes super itchy self.

  • So all we're doing this for is because generally, if we display this object in, like, print out the object, let's say it's going to look kind of ugly.

  • It's gonna be like something tutorial object.

  • And instead, what we should say is, as we generate 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.

  • Any time you create a model, basically, a model is going to map to a database table.

  • So this is our we've got her database already.

  • Basically, you already should have a database.

  • I'm trying to decide where it would be pride back here.

  • Yeah, Here's your database.

  • Traitor.

  • DBS Key light three.

  • The default is sq light later.

  • You can change that if you go toe my sight.

  • Ah, settings dot pie.

  • Uh, and then somewhere in here, it'll be somewhere in here Anyways, over here, probably.

  • Yeah.

  • So this will be your database engine.

  • You could change.

  • It s key.

  • Light is great for development and tutorials like we're doing right now.

  • You can scale and sq light database actually quite well, So I wouldn't you know, the conventional Internet wisdom will tell you I never use sq lighting production.

  • Maybe escalates, actually pretty darn powerful.

  • It's just that my SQL or something like that, or even some of these other databases is probably a better choice in your deployment.

  • But, uh, well, we can worry, even talk about that way later.

  • Down the road, escalate three is nice because it comes with Python.

  • Nobody has to do installation, and everybody can follow along no matter of the operating system, so we'll keep escalate three.

  • And anyways, you already have your table.

  • So what this is gonna do is I'm sorry already.

  • Ever database, What this is gonna 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, um, you will already have.

  • Ah, primary.

  • He is just default done for you.

  • But you could also have 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, uh, I'm not running a server anymore.

  • Uh, S o python managed up.

  • I run server.

  • Uh, 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.

  • Um, 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 sight.

  • Right Eye sight?

  • 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 was only one time you'll run that migrate first.

  • Uh, So what we're gonna say here is basically every time you add or change, So if you add another model where you want to change a pre existing model, you have to make migrations than my great, 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 up high 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 you're slow.

  • You would be stunned for a few minutes.

  • So what happens is any time 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, you Earl's 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 your explicitly pointing But anyway, I don't know, it feels funky to be, But any time 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 pie actually was already open, and then it should be near the top.

  • Basically where we, um yeah, right, right here.

  • This is where you're gonna add any new APS 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 aps dot maine Coon fig switching, you add should immediately ask yourself, Does main APS mean config already exist?

  • Well, we can actually check that really quick main APS boom.

  • Here we are.

  • Main config name.

  • It'll make great.

  • So what already exists sounds good.

  • So as long as settings has saved, will 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 global, blah and all that.

  • Also, you can come into your directory here and then Now we've got this migrations and you kind of see, like what its plan is for the future.

  • If you open that, I don't know what 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.

  • Uh, 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 up.

  • Pie s que el migrate Maine, and then you get the i.

  • D.

  • So in this case, it was 0001 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, sq a lot.

  • 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 python manage up high, make migrations, we can run it again.

  • It's just going to say nothing new.

  • Then all you were going to run his python managed up high migrate.

  • So this time it did quite a few things on Lee 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 gotta run, make migrations and then migrate again.

  • Like I just really stumped me.

  • All you need to know.

  • You 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 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 up 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, for example, let's say we make some tutorials and we want to generate over the tutorial.

  • But something's going wrong and especially like later on, when we have, like, foreign keys, that air pointing at different tables and we're trying to figure out okay, how can we pull?

  • And how do we write the statement that we weren't right.

  • And if you're trying to, like, build a view in a bar, 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 very tedious.

  • So instead you could use something like the shell.

  • So just real quick we can do something like this, we could say from Maine from man main dot models, we want to import our tutorial model.

  • Then what we could say is like to tour you'll dot object stock ball.

  • And this is the exact same syntax that you would actually use in.

  • Like, for example, your views.

  • DuPuy, you wouldn't say from Maine.

  • It would be a relative import.

  • But anyways, you would do something very similar to this, at least and actually tutorial objects.

  • All this is something that you would do.

  • So, um, because you could do like all you can filter you can get, you could do all kinds of cool stuff, All of which I think, all of which will end up actually using So anyways, um, in this case, we see there is nothing here.

  • There are no tutorial objects.

  • All we did was just create the table of the database.

  • So we would need to, like, added tutorial in here so we can definitely do that.

  • We have tutorial title content and published so real quickly we could just add one in.

  • So we could say we create this like, new tutorial object.

  • I would say that is equal to a tutorial.

  • And then we'll say tutorial title equals Toby.

  • Can anybody guess where this is going?

  • Tutorial content equals or not to be.

  • And finally, we should have button 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.

  • Um, I think I'm gonna let it throw it syntax, error, and then I'll just use the up Barrow because I guess I could copy it, too.

  • But I'm not really worried about that.

  • Then what we're gonna say is 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 jango dot You tills 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 and you can you can build an insertion.

  • But then at the end of the day, you have 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 do tutorial about safe.

  • Now we actually have it.

  • So now if I just up arrow all the way through objects all boom, we've got one later we could generate over that.

  • So for tea in tutorial dot objects Oh, don't all 23 4 France t dot tutorial title, for example.

  • Lips.

  • Did I see?

  • What did I do wrong for tea and tutorial t down to what I do run there.

  • I feel like I've got to be, like, totally blind here for tea.

  • Oh, Capital T tutorial is my issue there.

  • It was like, when did I use tutorial?

  • Ok, print t dot tutorial title.

  • I did it.

  • Okay, great.

  • So, um, again, this is the same sort of, uh, syntax and logic that you would use to actually it right over your objects in save used up I 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, um, I'm gonna leave it here, and I think the next tutorial 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 that I can show you guys the admin page, which is again, One of the reasons why your models are just incredible.

  • That we have in the admin page from Django is, um, 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, um, yeah, so questions, comments, concerns, Whatever feel free to leave those below.

  • A quick shout out to my most recent channel members, Rhodolfo said.

  • K G one son and Ali, 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, uh, that's it for now.

  • If you didn't know how to become channel member, you click that beautiful, beautiful blue blue.

  • But in there, that's it.

  • For now, I will see you guys in another video where I continue showing you guys the Mattrick of Django.

what's going on?

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

模型 - Django Web開發與Python p.2 (Models - Django Web Development with Python p.2)

  • 0 1
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字