Welcome back, everyone, in this lecture, we're going to discuss the Django model form class.
Now, often we're really using forms to directly interact with a model.
We've already explored how to create a form that can just accept general information and then we can
do whatever we want that information.
But we're often using that to do things like create a new instance of a data point inside a model.
This is common enough that fortunately, Django provides the model form class, which automatically
creates a form with fields connected to each model field.
And you can directly call things like save off of the form, which would save a new instance to your
models in your database.
Let's check this all out.
Insider Code Editor OK, here I am inside Visual Studio Code.
Here is our form.
And technically speaking, when somebody submitted this form over in views that pie, we could do whatever
we wanted with that information through the form.
Not clean that data dictionary every time someone passed in an actual form.
I've just been printing it out.
But technically speaking, I could have done something like save that to a model that already existed
on the back end.
However, the reason I didn't really show that too much previously is because if we do plan on using
forms with models, it makes a lot more sense to use Django's built in model form, a class that is
going to automatically generate a form based off the fields inside a model.
Now to do this, we first need a model.
So we'll come back to explore.
Open up models that pie inside the cars application, and let's create our first simple model.
So we'll create a model called a review model.
So same models thought model inheritance, and we'll just have this be a little simpler than before,
we'll still accept the first name, so say models, a character field and let's say maxlength a 30.
Let's also accept a last name for the person giving a review.
So models again character field maxlength 30 and then just a change it up.
Let's imagine this time we only want to accept stars like a star rating between one and five stars of
their experience.
So we'll create a attribute.
Stars say models, and let's actually make this one an integer field.
So we have our model.
And what I also want to do is register this model inside my administration.
So how do I do that?
What I need to do is come to the admin that pi file.
So I need to open this up and then open up admin that pie.
And this is where we register a model.
So I can say something like from that model's import, the review model we made and then register it
with the admin or say, admin site.
Register and then register the review model.
OK, so I've created my model, but technically speaking, I haven't actually really registered it with
the back end yet because I haven't run migrations for it, so I still need to do that.
So I'm going to say python managed pie make migrations.
And what looks like we have an indentation error inside models will come back here, and let's take
care of that indentation.
It looks like I had a little space there and everything is off by one space.
My apologies there.
OK, now let's save that.
My background was too dark here.
Really?
Actually see that.
So let's try it again.
Python managed up pie make migrations perfect, so we initially created that review model.
Now I'm just going to run the migration.
So say Python managed pie migrate.
Perfect.
So now I should have a car on my actual back end in order to make sure it's actually working, let's
create a super user so I can access the results from the admin page.
So we'll say Python managed the pie, create super user.
And the username, I was going to say, it's my admin email address will be my admin at Edmunds.com.
Password will be my.
Admin password.
As we've mentioned before, it's technically invisible, so we'll enter their password again, my admin.
Password, hopefully, you didn't mess that up.
OK, perfect.
Super user created successfully So now I have a way to actually check on the back end of the fact that
it was registered there and we actually did some changes.
So let's first confirm that that is working.
So I'm going to go to my website and then visit the actual administration view and let's actually run.
This will say Python managed up pie run server.
OK, so our server is now running.
I'm going to open up my Django admin view, so now I should be able to say my username, so my admin
and then my admin.
Password.
I'm going to log in now and tonight I see groups, users, the default authentication authorization,
but now I also see cars model so I can see reviews.
And if I wanted to, I could add a manual review here first name, last name and then maybe give it
some stars.
So like one, two or five stars.
Notice right now I can actually go above five stars.
We're going to talk about errors and validation in the next lecture and customization.
But for right now, I have, let's say, four stars.
I can go ahead and save that, and I have my review object.
OK.
So obviously, I want the user to be able to provide that within a form.
So what I could do is go through a very manual process of matching up a custom form here, making sure
the fields match up and then later on inside of views that by having to make sure that I manually save
form clean data by actually grabbing those things like, OK, grab the first name and then save that
to the instance of a model, etc. But luckily for us, because this is so common, Django's already
thought of something for us.
So what this is called is a model form.
How does this work?
Well, I come back to the forms that file.
So we'll come back to forms that PI file, and what I'm going to do right now is just comment all this
out.
So I'm going to basically not use that review form anymore.
And instead, I'm going to say from that model's import, my model, which was the review model, then
I'm also going to say from Let's make sure we don't do that on all caps from.
Jane, go dot forms.
Import the model form.
OK, so this is essentially going to be like forms, but instead of that form, it's forms dot model
form, but right now I'm just importing it like this so I can just pass in the model form.
So let's show you how to essentially create a model form, which put simply looks at this model, looks
at the fields and then will basically automatically generate something that looks like this, which
automatically generates widgets, which automatically generates a form.
So we're going to again say class.
Let's keep the same name review form, so I don't need to worry about import changes later on.
And now this is going to inherit from model form.
And then all you need to do here is have a subclass called Meta.
We say class meta where it has a single attribute for now and we say model is equal to review.
And that's it, and it's kind of like magic, but really, we can kind of tell what it's doing, all
it saying is, Hey, I have a model.
Go ahead and make a form for it.
So what I would encourage you to do at this point in time is also check out the documentation from this.
So it's called creating forms from models, and it just talks about the model form and what we discussed
here.
Again, super easy.
You just essentially connect it with model.
And what you'll also notice is you can actually choose which fields should be represented inside the
form.
So if we come back here, we see that we have the attribute model, but maybe you don't actually want
to collect everything.
Maybe I only want to collect something like the first name or last name.
So what I could do is come back to forms.
So I come back to form that pie here and then say fields as my attribute and you just pass a list of
the strings you want, such as first name and last name.
Go ahead and save that change and remember review form that's still being imported over here and review
form.
And now I'm still just going to print out my form clean data, and I should be able to see things inside
my actual rental.
You.
And you may have noticed we got a little air here, it says, oh, by the way, you forgot to actually
create the fields attribute or exclude.
So it's telling us, Hey, this needs updating.
I'm going to refresh the page.
I may need to restart the website if this didn't work.
So, yeah, let's go ahead and hit control.
See, that's one of those errors where you just have to rerun the website.
There we go.
Python managed up high.
Go ahead and run that server again.
And you should see now a very simple rental review form that only accepts a first name and a last name.
And then if we hit submit query, we see, thank you.
But notice over here we should see that actually printing out first name and last name.
Now, obviously, we just connected this to a model.
So what I could do is come back to forms and say, Hey, when somebody submits this form, actually
want you to create a new instance of a review.
Now for that, we should probably add in the stars.
So we'll say first name.
Last name.
And add in stars save that, so now I should see all those fields inside my form.
So if I come back here and refresh, I see first name, last name and stars, which is an integer field.
Now, later on, we'll talk about errors and validations and that sort of thing.
But for right now, I'm going to say first name, last name stars hit submit query and that should say
thank you.
And again, we see it printed out here.
First name, last name stars.
What you want to do now is save that as a new instance within your models, and then we can go ahead
and go to admin to check that it's actually all working.
And luckily for us, it's super easy.
All we need to do is come back to views.
And instead of saying something like print a firm, clean data, I just say OK, if that form is valid.
For example, the length of the names weren't more than 30 characters.
I simply say form.
Not safe.
And this single line, because it's a model form, is going to automatically save what the user passed
in as a new instance of the model.
Let's check it out.
So we'll come back here.
And remember, I'm still logged in here in my Django admin view with reviews and currently only have
one review object.
So we're going to come back to home here.
And again, I'm going to refresh my rental review form.
So let's give this a first name.
The check, I'll say Jose Portela.
Let's say I had a five star experience.
I'll go ahead and submit the query.
It says, thank you.
And now if I come to the admin page, I'm going to go to reviews.
No, I have a new review object and it's the one I just applied.
Jose Portier five stars.
Perfect.
So look how easy it is now to let a user directly interact with creating new models.
So we just need to say the model.
Then we come to forms and call model form and then underneath class metta.
You connect it with a model and you connect the fields in that model, you actually want to interact
with and know how.
This is why the original form had fields that looked a lot like the model fields like character field
here was pretty much character field here.
And what it does is it does a mapping of the model fields to the relevant form fields.
And if you actually look up in the documentation the model forms, it actually gives you a table of
what it maps to.
So if we come back to the Django documentation creating forms for models, you'll see field types.
Essentially, there are certain model fields that are represented as farm fields.
So, for example, we can see character field for model field is represented by a character field for
a form field, and you can see most of them are kind of one to one.
There's date field eight field email field email field.
Some don't actually have representation in the form like auto field or big auto field.
Those are typically used for the primary key index, and that's not something you actually want to represent
in the form.
You don't want some user to be able to almost like hack into your database and rewrite primary keys.
That's why you don't see auto field and big auto field as form fields.
But here you can see the mapping of the model field and the form field.
I should also mention there's a ton more information here on again specifying which widgets to use.
So a lot of it looks kind of similar.
Again, you're saying widgets, then you say, OK, make this a text area, then make it the attributes,
etc. There is a little more work you have to do.
You have to use something called model forms that underscore factory.
I'll go ahead and let you explore that documentation on your own.
It's not so bad.
You basically just come here and create a new model form set.
Again, it's more and more customization, depending on what you want to do here, for which widgets
in general we're trying to save ourselves work.
So often the default choices are good enough for us, but that was the way you could automatically connect
a model form with a model in the backend, and we just saw that work together.
Now I want you to keep in mind when we learn about class based views, that's going to give us even
more power to automatically render an entire web page that specific for creating, reading, updating
and deleting items from a model.
So don't think we've kind of reached the pinnacle of Django yet?
It gets even better kind of all building off of this idea of model forms and the idea that we often
track website for form on the front end and a model on the back end.
Coming up next, what I want to talk about are a bunch of other attributes that are going to be useful
in customizing our model forms.
But again, I want to make it clear when we're talking about things like how do I use a model form to
maybe delete an instance for a model or update an instance for a model?
Well, you can't technically do that with a model form.
I would highly encourage you to wait until you learn about class based views before you head down that
direction, because class-based views in the next section are going to make that even easier than using
model forms.
So trust me, it gets even better than this, even though this is already pretty darn good.
So I'll see you in the next lecture when we talk about customizing model forms.