Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Update and Delete Data | Working with Database
Django: First Dive
course content

Contenido del Curso

Django: First Dive

Django: First Dive

1. Get Started
2. Write the First Page
3. Models
4. Working with Database
5. Templates
6. Request Handling

bookUpdate and Delete Data

Update data

If you had no problems with the Create and Retrieve operations, you should be able to easily understand the logic of updating data. Let's break down the logic into certain steps:

  1. Extract the object and write it in a variable.
  2. Reassign attributes.
  3. Save the changes using the save() method

Let's look at the example.

URL pattern

View function

Result

The provided code updates the Post using pk (id) received from the URL address.

Delete Data

To delete data, you just need to extract data and use the delete() object method.

URL pattern

View function

Result

You can see that None is displayed instead of post id. The fact is that the object was already deleted by the delete() method, and, accordingly, it does not have an id because it is no longer in the database.

To display the id of the deleted object, you can store it in a variable and then output it using HttpResponse.

1. Which method help us to submit changes (create and update) in the database?
2. Which method help us to delete record in the database?
Which method help us to submit changes (create and update) in the database?

Which method help us to submit changes (create and update) in the database?

Selecciona la respuesta correcta

Which method help us to delete record in the database?

Which method help us to delete record in the database?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 3
some-alt