Welcome back, everyone, to this lecture, which is going to discuss the update view, which allows
us to update an already existing entry inside our models, similar to create view, except for something
that already exists.
Let's get started.
All right.
Here I am back inside views about pie.
One thing to note when we talk about update view is it's kind of a mix between a detailed view and a
create view.
It's kind of like a create view because you will be filling out a form to up the information.
But it's also kind of like a detail view because when you're talking about updating, you're talking
about a specific entry with a unique primary key.
So you can think of an update view as a create view that already pre filled out the form based off a
detailed view.
Let's see how it works.
I'm going to import update view from Generic, and then let's create our new update view.
So we'll say the following class.
Teacher up, the view, we inherit the up the view and then we say model is equal to teacher, so we
connect it to a model and then technically there may only be a few fields you want to update.
However, you should note the following is that Teacher Update View is actually going to share the model
form HTML template that create View also uses, and it looks a lot like create view as far as attributes
are concerned.
Now, let's take a moment and think, does this actually make sense?
Should it make sense that the creation view uses the same form as the update view?
In my opinion, it's yes, that does make sense, because if you're creating a new instance of a teacher,
you should have all those fields available for you when you want to update a teacher.
Now, maybe you want to limit the fields upon updating.
You can technically do that.
You could say fields and your update view is equal to and then pass a list.
For example, maybe you're not going to, for whatever reason, allow people to update their first name
or the subject they teach.
But maybe you want to be able to update last names in case somebody gets married and changes their last
name.
What you could do is have an update view that only shows the last name on the form, and you could just
pass on a list of the fields and the names you want to update.
And remember these names here.
Those are just from the model, so I connected it to Model Teacher.
I go to models and I see, OK, I could update their first name, last name, subject, or if you just
want to be able to update all of them, you would come back to views and just like we did for create
view.
You would just as a string pass, an underscore underscore all that says allow all the fields to show
up in the form that's automatically filled out and created for update view.
And then lastly, similar to our creation view, remember, upon clicking submit for the update, we
have to take them to a particular page so we could use the same idea of success.
URL reverse lazy to do that here.
An update view.
OK, so we say success early, except maybe we take them to the list teachervue, because that would
make more sense to actually view the changes.
So this way we'll update you does is it's actually going to open up the.
Teacher form and then display the form that you sent back, obviously, as I mentioned, you technically
have flexibility over what fields show up an update versus what fields show up and create, and in certain
situations, that actually makes a lot of sense.
Do you have limitations on what can be updated versus limitations on what can be created?
Maybe there are some sort of date time stamp for creation that you don't want someone to be able to
kind of go back in time and then say, actually update the time stamp here?
Maybe only one admin user to do that from a super user standpoint.
Again, it really depends on the situation, but I do want you to kind of think about those cases where
in this particular case, we're saying you can update any field you want first name, last name or subject
that's being taught.
Maybe in your particular situation, you actually only want to update a few fields.
And in this case, you define the success URL again, notice just how similar this looks to create view.
Now, it's not exactly like create view, because remember when we're updating something, we're only
updating again a single primary key, which means that you URLs is actually going to look a lot more
similar to something like a detailed view.
Let's check that out.
I'm going to go to my URLs that pie and then let's import our teacher update view.
And let's create a new pathway here.
We're going to see path is equal to.
And we're going to call this update teacher forward slash.
And this is where the primary key is going to go into your primary key.
And then we just say teacher update view as a view.
And if you want, you can give this a name as well, like update teacher.
OK, save that change.
And so now it's going to happen, is it going to have the capability to update a teacher?
Now we're should actually link this again.
It kind of depends on what you want to do.
You could if you wanted to create a new template that says, Hey, fill me out for a form to link you
to an update where they just fill out a form, maybe pass in one, two, three four, etc. That's a
primary key and a link to update a teacher.
You can't again really have a generic update teacher link that doesn't really make sense, because remember,
you need a specific primary key.
So maybe a good candidate for that particular link would be similar to where we passed in the teacher
list.
I could say maybe a link here for updating.
So let's create yet another anchor tag, and I'm going to put this all within the same list item.
So we're going to say teacher, first name, last name and then.
This is the list item, but I'm going to create yet another anchor tag here, and it's going to go to
classroom.
Update teacher.
And then it's going to give the teacher ID.
Very similar to a detail to you, except now we're linking them to that update page.
And then here I can just put some text to say update information.
Obviously, you can get a lot more fancy if this maybe this is a button or something like that, but
it's going to save that.
OK, so let's see what our list looks like now.
So I come back to our list.
I refresh this and now we have Adam Smith and then it says update information.
Now I see up the information three times here.
Maybe that's not super clear.
So what I can do is come back here and I also accidentally delete a space there.
And so I can stay up the information for.
And then maybe I say teacher first name.
Something like that again, this is just a team of formatting on what you want to do here.
It's not so much jingo.
So I refresh this and maybe this looks a little nicer.
Arrowsmith Smith up the information for Adam.
Maybe I actually have that as an indented list item, so you could keep kind of messing around with
this to get the best kind of look you're going for.
So I can make this yet another.
Unordered list here and then stick that in as its own list item, so I could copy that passenger list.
Puts that in.
And then let's make sure our tags are close correctly, so this one would go this closing tag.
Would go here.
There we go.
And there we have it.
OK.
Let's see if I hopefully that it must add up.
But essentially it's trying to deal with a nested list and there real Adam Smith update information
for Adam Jose Padilla of the information for the third teacher up the information officer.
OK, not so bad.
Now let's see what happens when I go to Adam Smith.
Remember right now if I click, Adam Smith takes me to the detail view.
Adam Smith teaches economics.
Let's say Adam Smith went ahead and changed the topic.
They were teaching how they want to teach math.
I click on update information for Adam.
And it takes me to that teacher form.
If you look carefully at the URL for this, it says Classroom for update teacher forward slash number
two.
So Adam is the number two teacher.
I ended up creating its primary keys, too.
Then I can come here, it's economics and maybe switch it out to math that I hit submit.
And now we have the list of teachers and the list you.
Adam Smith Jose Padilla, third teacher If you click on the detail for Adam Smith and now says Adam
Smith teaches math.
So what's happening behind the scenes?
Well, when you click on an update view for a particular primary key, it essentially just calls the
teacher form.
But unlike Create View, a update View says Fill it out or pre fill the information that is already
known for this particular entry at this particular primary key.
Then, upon hitting submit, you're going to save those changes an extremely useful view.
Again, all we're doing here is connecting to the same form we had for create view.
Except this time I'm pre filling it with the information for a particular primary key.
That means the same caveats from the details you apply to the update view.
I can only do this for a particular primary key.
I can't just say Hey Link to the update view.
I want to point out what I often see students do as a mistake is on the homepage.
They'll try to make a new link here that says something like anchor tag classroom or URL.
So I'll say something like URL.
Classroom and then they just try to say something like detailed teacher or something like that.
Or maybe they try to say update teacher, but this doesn't really make sense to be on the home page
because I need a primary key.
And if I just have this is a link, there's no information of what teacher actually want to update.
So if you want to make a generic link like that, that would have to first take you to some sort of
form where the client can inform you.
I want to update user number one or user number two, user number three, etc., which is why typically
it's kind of nicer to combine the idea of this list view with the fact that I know that it is already
there for me, rather than have the user maybe provide accidentally the wrong idea.
Of course, it's up to you and the website you're creating, but you get the idea.
All right.
Hopefully, you see now how powerful these class based views were.
And we're going to discover one more class based view, which is the delete view.
And it's essentially like update.
Except this time, instead of updating, it's going to delete that particular entry.
Let's check it out in the next lecture.