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