Welcome back, everyone, to this lecture on updating existing entries in a database.
Now, luckily for us, Gyngell makes it super easy to update an existing entry, only to do is grab
the existing data entry, update any attributes by simply overwriting them just as you would any other
attribute for a Python class object.
And then you call Dot save on that object, and then those changes are going to write that update to
the database table.
Let's check this out in our code editor.
All right.
So here I am inside a visual studio code.
Remember, previously we updated the heart rate as a new attribute and left a default value of 60.
Now, let's imagine I wanted to grab a specific patient and maybe adjust their age.
Or maybe they got married and they changed their last name or adjust their default heart rate.
I would actually update that entry.
Well, what I'm going to do first is actually begin the shell, so I'm going to call Python manage the
pie shell.
And now we have the shell that I can work with, keep in mind, is running an eye python, yours, maybe
just running in bass python.
But now that I have this, I should be able to actually import models and then begin to grab the patient
model.
So I'm going to say from office models.
Import patient.
And then how do I grab a particular patient recall, I can say something like patient objects, and
then maybe I want to get one where the primary key is equal to one.
So just the very first patient and that returns Carl Smith, who's 30 years old.
Let's say Carl decided to change their last name.
How would actually update their entry in my actual database?
Well, I would first need to assign this actual instance of the object so I can say something like Carl
is equal to and then again patient that objects that get primary keys equal to one.
Obviously, there's many different ways I could either filter or get a list of entries I wanted to update,
etc. just doing it right now with a single entry.
And then what I can do after this is check out Carl, who again is the patient and I can check out Carl's.
Last name.
And it's Smith, let's say I wanted to update Karl last name, remember, Karl hears just the variable
I could call this person.
It doesn't really have to be their first name, so I can say Karl last name and then simply reassign
it.
So maybe we are operating something like, again, a patient doctor's office.
Someone gets married, they change the last name.
And so now let's go ahead and change their last name to, I don't know.
Let's say they call it jingo.
So now Karl's last name is equal to jingo, so if I call Carl notice it says Carl Jingo is 30 years
old.
Remember, we were pointing out the last name first, according to this string entry.
Now, this hasn't actually changed anything.
We've only changed this object that we've been playing around with.
However, this object does have information of what primary key it's in regards to the database table.
So if I want to save this change of Carl's last name being jingo now, I simply need to call Carl Dot
Save Enter.
And that actually writes the change to the database.
In fact, let's check this out.
Make clear this.
And I'm going to say patient the objects, and let's just grab all of them, so when I say patients,
the objects all.
Now, you see the very first patient here is General Carl.
So it's pretty incredible that general gives you the flexibility to just grab any entry in that database,
reassign those attributes just as you would if any other Python object and then you want to save that
change or right, it's a database.
You simply call that safe.
That's going to give us a ton of flexibility inside of views that pie to actually interact with the
data, which we'll see later on.
But that's pretty much it for updating an entry in a database.
Come up next.
Let's talk about removing items or deleting them.
I'll see you there.