Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Adding Instances6:58 with Kenneth Love
We have a model and we've run our migration, but how do we get data into the database without writing SQL directly?
python manage.py shell opens a Python shell with Django's configuration already loaded.
Model.save() will save an in-memory instance of a model to the database.
Model.create() will save an in-memory instance of a model to the database and return the newly-created object.
Model.filter(attribute=value) will get a QuerySet of all instances of the Model that match the attribute values. You can change these values with other comparisons, too, like
in. Find out more here
So we have our model created and migrated, 0:00 but we don't have anything in the database yet. 0:02 So how do we go about adding things into the database. 0:05 Well, we can write a Python script that creates new instances for 0:08 us but, that's not really as friendly for just playing around, as I'd like and 0:11 thanks to some configuration requirements that Django has, 0:15 it's actually not that straightforward. 0:18 We'd have to do a whole bunch to set it up. 0:20 You should know by now that I don't ask you these questions without a solid answer 0:22 already in mind. 0:25 Django has another handy command for manage.py that lets us 0:26 go into a Python shell with Django's configuration bits already loaded. 0:29 We can use this to explore the ORM and create some new records. 0:33 Let's go try it out. 0:37 So the command that we want to run is, 0:38 let's make this bigger here, python manage.py shell. 0:41 And you can see this looks just like a regular python shell, and it is. 0:45 But it has Django's functionality loaded, it has our settings, all that kinda stuff. 0:52 So let's import our model from courses.models import Course. 0:56 Now when we want to do a query with Django's ORM, 1:04 we have to use the model name and its objects attribute. 1:08 Objects points to what's called a model manager, 1:11 which is a class that controls access to the model's instances and other things. 1:14 We're not going to get into models managers in this course, 1:19 it's a wonderful, deep area. 1:21 So, just trust me that they're there, and we'll get to them eventually. 1:25 The most common query is to get all of the instances. 1:30 So, let's see what we get when we do that. 1:33 So, we'll say, Course.objects.all and 1:36 we get back nothing because we haven't created anything yet. 1:39 So we get back what's called an empty query set. 1:44 It looks like a list, but it's actually a query set. 1:47 If we do type on this, see, we see that it's a query set. 1:49 It's not a list even though it looks like one. 1:56 Okay, so let's create one. 1:58 So we'll say C is a course, and 2:00 we're gonna say C dot title is python basics, and 2:04 we'll say C dot description is Learn the basics of Python. 2:08 So at this point c is a course, but it's not in the database yet. 2:15 We have to call the save method to actually put it into the database. 2:20 So c.save. 2:25 Now we can find it in the database, so we'll do course.objects.all again and 2:27 there's our course, course object, so great. 2:33 We got back a query set with a single item in it, 2:36 but course course object isn't very descriptive, is it? 2:39 We'll change that in a second. 2:43 We went through a bunch of steps to create our instance. 2:45 We had to create the course, we had to add the title, the description, 2:48 we had to save it. 2:50 We can do that quicker, so, let's do so. 2:51 I'm gonna exit out of this and 2:54 restart the shell just to make it easier for everybody to follow along. 2:56 So, come back to here from courses.models import Course. 3:02 Let's make this bigger again. 3:09 Okay, so, the last time we did it, we did the c equals blah blah blah, 3:12 and it was a lot of steps. 3:15 So, let's just make one all-in-one step. 3:17 So, Course(title="Python Collections", 3:20 description="Learn about list, dict, and 3:25 tuple"), and then we'll call dot save on that. 3:30 Okay so now if I do chorus.objects.all again now I've got two items. 3:37 Now I don't know which one's which. 3:41 The first one's probably python collections, 3:43 the second one's probably python basics, but we don't know. 3:45 But again though, we're making one in memory, 3:49 we're creating this object in memory, here. 3:52 And then we're calling save on it, and that works, 3:57 I mean we just did it twice, it obviously works, but it's two steps. 4:01 So we can do it all in one step, let's do it all in one step, okay, 4:05 and this way we'll have three things in our database. 4:08 Three is better than two, right? 4:11 Okay, so we do course.objects.create. 4:12 Title equals object oriented Python. 4:18 Description equals learn about Python's classes, 4:26 Return and you notice, when we did Save here, and 4:32 when we did Save before nothing, nothing comes back out. 4:35 The next line immediately goes back to a prompt. 4:38 Right, but when I did Create, I get back an object. 4:41 So, create always returns an object for 4:47 me, which is handy depending on what I'm doing. 4:49 Okay, so now let's check out got, 4:51 Course.objects.all, three objects, that's awesome. 4:54 Okay, so we've got a few items, let's update the presentation of our models. 5:00 So we're gonna do Exit or quit to get out of the shell. 5:04 And then we're gonna go back up here, 5:07 we don't need settings and we don't need initial. 5:11 Let's go back to models. 5:13 So onesidemodels.py, and our course is app. 5:15 So what we can do in our class here, is we can define a str method, and 5:19 if you remember from object-oriented Python or if you didn't take that, 5:25 thunder STR defines how this thing turns into a string. 5:32 So Django uses this whenever it prints out a reference to an instance, so 5:37 in the shell and some other locations when it's just like hey here's the thing, and 5:42 here's the way to describe it, this is what it uses under STR. 5:47 So let's use this instead of course course object, 5:53 we want something that makes sense. 5:56 So what makes sense? 5:58 Well let's just return self.title cause the title is what the thing is. 5:59 Right? 6:04 Okay, so now we come back down here to our shell. 6:05 And from courses.models import Course, 6:15 and Course.Object.all, and check that out. 6:21 We've got course python basics, course python collections, 6:28 and course object oriented python. 6:32 So that's pretty awesome, 6:35 that's a lot easier to read, a lot nicer to find your way around. 6:37 We'll play with more ORM queries as the course goes on, and 6:40 of course in future courses. 6:44 Let's leave the shell alone for 6:47 now though and build a view to show off our newly created courses. 6:48 If you want to explore the shell more, check out the teachers notes for 6:53 some more queries and links to the ORMs documentation. 6:55
You need to sign up for Treehouse in order to download course files.Sign up