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

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

path("posts/<int:pk>/update/", post_update, name="post-update")

View function

def post_update(request, pk):
    post = Post.objects.get(pk=pk)
    post.title = "Updated Post"
    post.text = "This post was updated"

    post.save()

    return HttpResponse(f"<h1>Post with id {post.id} updated</h1>")

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

path("posts/<int:pk>/delete/", post_delete, name="post-delete")

View function

def post_delete(request, pk):
    post = Post.objects.get(pk=pk)
    post.delete()

    return HttpResponse(f"Delete Post (id {post.id})")

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.

def post_delete(request, pk):
    post = Post.objects.get(pk=pk)
    post_id = post.id  # new
    post.delete()

    return HttpResponse(f"Delete Post (id {post_id})")  # updated

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?

question mark

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

Select the correct answer

question mark

Which method help us to delete record in the database?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Mi faccia domande su questo argomento

Riassuma questo capitolo

Mostri esempi dal mondo reale

Awesome!

Completion rate improved to 3.45

bookUpdate and Delete Data

Scorri per mostrare il menu

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

path("posts/<int:pk>/update/", post_update, name="post-update")

View function

def post_update(request, pk):
    post = Post.objects.get(pk=pk)
    post.title = "Updated Post"
    post.text = "This post was updated"

    post.save()

    return HttpResponse(f"<h1>Post with id {post.id} updated</h1>")

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

path("posts/<int:pk>/delete/", post_delete, name="post-delete")

View function

def post_delete(request, pk):
    post = Post.objects.get(pk=pk)
    post.delete()

    return HttpResponse(f"Delete Post (id {post.id})")

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.

def post_delete(request, pk):
    post = Post.objects.get(pk=pk)
    post_id = post.id  # new
    post.delete()

    return HttpResponse(f"Delete Post (id {post_id})")  # updated

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?

question mark

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

Select the correct answer

question mark

Which method help us to delete record in the database?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 3
some-alt