Maybe you’re new at using Django and you’re just starting to get familiar with the
Let’s say you have a
ModelForm for a
Customer that looks like this:
You then put this form on a template and render it out so your users can fill it out and update their information. When your user submits the form, what does your
View look so that your can update their information?
Maybe, you’re thinking that you can do something like this:
- Essentially, what you’re trying to do is see if the user exists
- So you grab the customer from the database.
- You get the
- After, you try saving the form with
commit=Falsewhich prevents the data from being saved to the database.
- And at the very end, you update the
customervariable that you got from the database with what’s in the form.
That’s okay to do if you’re new to ModelForm and it’s explicit in what you are trying to do which might make it easy to read. However, this isn’t how you do it with Django. There is a MUCH easier way. Ready?
Easy? You can read more about ModelForms from this part of the documentation (especially if you have much more complex forms). Next time you’re working with ModelForms, try this simple way of updating model data.