When you have finished validating data in the Form, the data can be saved in the database. To save the data in the Form to the database, use the save() function of the Form instance. The save() function of the Form object works the same way as the save() function of Model objects that was discussed in Hour 5, "Using Data from the Database in Views."
The save() function preprocesses the data in the Form, prepares the data to be inserted into the database, and inserts the data into the database. The Form save() function uses the same database table, so as long as the primary key matches, the object in the database is updated.
Try It Yourself: Save Form Data to the DatabaseIn this section, you will add save functionality to the person_form() view function so that when the update button is clicked, the data actually gets saved to the database. Follow these steps to call the save() function on the PersonForm object created in person_form():
Listing 11.4. Full Contents of the person_form() View in iFriends/People/views.py
|