Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Delete An Entry4:43 with Kenneth Love
And, like anything else we do, eventually we're going to write an entry that we just don't like or need and we'll want to get rid of it. Let's use Peewee to remove records from the database.
There always comes a time when you have entries in your database that 0:00 you just don't want anymore. 0:03 Yeah, you could pop into a SQL shell and delete them, but where's the fun in that? 0:05 I mean, what if you delete the wrong row? 0:08 Everyone breaks their production database eventually but 0:11 let's not let that be you or me today. 0:13 I wanna be able to delete an entry that I don't want anymore. 0:17 And while, I mean, I, I suppose we could do like a delete entry in the menu, 0:21 and then you put in a number or something. 0:25 I think it makes the most sense to delete the entry while I'm reading it. 0:27 So, let's go. 0:31 You view entries. 0:36 Yep, our old friend view entries. 0:37 And let's a new, a new option so that we can delete an entry. 0:40 So we'll say d, delete entry, and then we'll add that to our action choices here. 0:45 So we'll put a d in there. 0:52 And then we've got this if next action is equal to Q, break. 0:55 Let's add an elif. 1:00 Next action is equal to d, and then, 1:04 let's call delete_entry and pass the entry that we're currently on, okay? 1:08 So, all we added was we added this delete entry line to our menu. 1:15 The D there just to make this, you know, hey, here's the stuff you can do. 1:20 And then if they sent us a Q we're gonna brake like normal. 1:23 If they send us a D then we're gonna call delete entry and 1:27 we pass the entry we're currently on to delete entry. 1:30 So let's come down here and look at delete entry. 1:34 Because delete entry takes an entry as an argument. 1:38 Now, the entry that comes in here 1:41 as that argument is the actual entry model instance from our database. 1:44 It's got all of the attributes that we're used to having. 1:48 Content, all that kinda stuff. 1:50 We don't really care about that though, we just want to delete it. 1:53 Right? 1:56 So, let's do that. 1:57 But, if there's one thing about deleting, 1:59 it happens sometimes when you don't expect it to. 2:02 So, let's make sure that the person actually wants to delete this. 2:04 So we'll say if input, are you sure? 2:08 And we'll do Y and then cap N. 2:14 [BLANK_AUDIO] 2:17 And we're gonna lower case that. 2:20 So if that comes back as Y. 2:22 So they have to specifically answer us with y. 2:25 Upper case or 2:27 lower case, it won't matter cuz we're lower casing it, but they have to say yes, 2:28 otherwise we're not gonna delete it cuz we don't wanna destroy anybody's information. 2:32 Then we can say entry.delete_instance. 2:37 And that will delete that instance. 2:39 So again, we're gonna make absolutely sure they want to delete it. 2:43 Nothing's worse than losing data just because you wanted to 2:45 play around with a couple of menus and see what happened. 2:49 So, the cool thing is, 2:52 the really nice thing is, this still runs inside of our view_entries loop. 2:53 So after we delete an entry, we'll get to see the next one. 2:57 This'll work even if we're searching. 3:00 So it all just keeps working together. 3:03 Let's try it. 3:06 So we come back down here, and diary. 3:08 All right, I'm gonna add a new entry that I don't really want. 3:12 I don't like this entry. 3:15 Let's spell things right even if we're gonna delete them. 3:18 All right, Ctrl + D, save it, all right, cool. 3:21 So let's view our previous entries. 3:25 So now we're on this one. 3:28 I don't like this entry, all right? 3:29 So now let's delete this entry. 3:31 So we type in D. 3:34 Are you sure? 3:36 Yes. 3:38 All right. It didn't print out anything. 3:40 Maybe we should print out entry deleted. 3:42 But anyway though, it's gone. 3:45 So let's go back to the main menu and let's view the previous entries, and 3:46 notice we're not on the I don't like this entry anymore. 3:51 So, let's add that in actually. 3:57 So that it prints out, entry was deleted. 4:00 So print, entry deleted, and save. 4:03 Let's test that one more time. 4:09 diary. Add on I don't like this entry either. 4:14 Yep, save that. 4:19 Okay. 4:23 View previous entries. 4:23 There's that one that I don't like. 4:25 Delete it? 4:27 Are you sure? 4:27 Yes. 4:28 And we got Entry Deleted. 4:30 So, nice. 4:34 Great! We can get rid of boring, useless, or 4:36 incriminating diary entries really easily. 4:38 We can clean up our database. 4:40 So now, let's see about cleaning up our interface. 4:41
You need to sign up for Treehouse in order to download course files.Sign up