Updates and Deletes6:59 with Kenneth Love
We've saved records through the admin and through model forms, but we haven't looked too much at how to do creation, update, and deletion operations with records on their own.
In Django forms we created an updated record through our forms and 0:00 that works really well. 0:05 Sometimes though we need to do it in the view or somewhere else in our code, 0:06 and we don't wanna create a form instance, or sometimes we need to delete a record. 0:09 The ORM gives us a couple of handy methods for updating or deleting records. 0:14 I'm here in our courses models and I want to have a published status for 0:19 our courses. 0:24 We should default those to false though so 0:25 that we have time to create a course before it shows up for students. 0:27 We're gonna add a new field and do that real quick. 0:31 So published = models.BooleanField(default=False). 0:33 And then we of course want to make that migration. 0:42 And then we want to apply that migration. 0:50 Okay, so now let's change our views and 0:57 our template tag to only show courses that are already published. 1:00 I'm gonna close some of this stuff so 1:04 it's a little easier to see what's going on here. 1:05 All right, so, okay. 1:09 Not .all filter(published = True). 1:12 Published = True. 1:19 In fact I'm gonna copy that cuz I'm gonna end up pasting that a lot of places. 1:21 They really shouldn't be able to get to that but we'll do that and 1:25 it is, Course__published = True. 1:29 And we'll do the same thing here. 1:40 Here we go. 1:48 And here. 1:51 And here. 1:59 We don't care about on the creates or the edits because I want to do this for 2:11 ones that don't exist. 2:15 And then let's do this here. 2:16 Published=True. 2:20 And we of course wanna do this here as well. 2:23 Cool. So 2:30 there's those, and we need to change our template tag as well. 2:30 So, Filter(published=True).latest, 2:37 filter(published=True) and I think we're good on the rest of those. 2:43 Wow, that's a lot of stuff, isn't it? 2:49 Okay, so now, let's go look at our Courses page. 2:51 I have to run my server. 2:57 No, my page is blank. 3:10 No, so we need to update the courses so that they show up. 3:13 We need to mark them all as published. 3:17 Into the shell we go. 3:19 Okay, so I have all my courses. 3:28 I mean I can do courses = Course.objects.all. 3:31 And what do I do now? 3:36 Maybe a loop right. 3:38 I mean I could do like the course = courses. 3:39 Course.published = True, course.save(). 3:44 I mean you saw me do that right. 3:50 And if I was to do course.objects.filter(published = True), 3:52 we're gonna get back that one. 3:56 I mean you saw me do this awhile ago with the subjects and yeah we can do that, and 3:58 we can do it in a loop. 4:03 But doing it on a loop isn't a great idea. 4:04 So whenever we do like this we're actually consuming the query set. 4:07 And then we do this, we're issuing a SQL command. 4:13 We're doing an insert or an update or whatever. 4:17 So we're hitting the database a lot, this isn't a great idea. 4:20 So what we can do instead is we can actually just do all of them at once. 4:23 So if we do Course.objects.update (published = True). 4:29 It tells us 15, 15 is how many objects it updated, 4:38 so objects.exclude. 4:42 So we have no courses that are not published 4:50 which he goes also gives us all of them. 4:55 So that's great. 4:57 We've updated all the records at once. 4:58 And we can call update on a query set, we can't call it on just a single 5:00 record it has to be on a query set and we did that course I'll just update. 5:05 There's an invisible all right here. 5:08 So it's affectively like us writing course.objects.all.update. 5:12 We don't have to put in the all because, 5:17 Django's like, you want to you want to all of them? 5:19 Right? 5:21 Cool. 5:22 We can avoid that all by putting in a filter or 5:22 an exclude in order to change how many records come out. 5:25 Using update like this is a much better solution than a loop that saves 5:29 every single record. 5:33 Okay, let's do one more exclusion thing here. 5:34 So our company has changed and 5:37 we've decided that we don't want to teach anymore courses that are about Cobol. 5:38 We had a couple of them, it's time to clear them out. 5:42 Cobol is just not gonna work anymore. 5:44 So let's do cobol_courses = 5:46 Course.objects.filter(subject_icontains="- cobol" and 5:49 make sure we get some stuff in here. 5:56 Yeah, we get two, okay. 6:00 So again, I mean how do we get rid of them, right? 6:02 We can do a for loop where we do know for course in cobol_ courses, 6:04 course.delete, and I know that we can do course.delete which is handy. 6:08 The cool thing about .delete though is that we can actually delete a single item 6:14 or we can delete everything in a query set. 6:18 So our cobol_courses is a query set. 6:20 So let's just do cobol_courses. 6:23 And you know what, let's let's find out who the teacher is, so 6:26 we'll know who to apologize to. 6:28 It's Craig. 6:30 All right, so cobol_courses.delete. 6:31 And now if we were to do Course.objects.filter(subject_ 6:36 icontains = 'cobol'), we have nothing. 6:43 All right, so no more Cobol courses and sorry Craig. 6:47 You know what I just realized, we added that published field to our model but 6:52 we didn't add it to the form. 6:55 Can you do that for me? 6:57 Thanks. 6:58
You need to sign up for Treehouse in order to download course files.Sign up