Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Update and Delete Operations | CRUD Operations
Django: Build Your First Website

Update and Delete OperationsUpdate and Delete Operations

In this chapter, we'll begin updating and deleting records from the database.

Deleting Records

AppFolder \ views.py
  1. all_notes = Note.objects.all(): Retrieves all Note objects from the database;
  2. for i in all_notes:: Iterates through all Note objects in the QuerySet;
  3. i.delete(): Deletes each Note object from the database;
  4. return HttpResponse('Notes deleted'): Returns an HTTP response with the text message "Notes deleted".

Updating Records

AppFolder \ views.py
  1. all_notes = Note.objects.all(): Retrieves all Note objects from the database;
  2. for note in all_notes:: Iterates through all Note objects in the QuerySet;
  3. note.title = 'LOL': Sets a new value for the title field of each Note object;
  4. note.save(): Saves the changes to the database;
  5. return HttpResponse('Notes updated'): Returns an HTTP response with the text message "Notes updated".

Please note that in update_content, we are changing the title field of each Note object to the value 'LOL'. You can adapt this code according to your specific requirements.

arrowfolder
__init__.py

__init__.py

folder/
folder

notes

folder

my_notes

folder

migrations

__init__.py

__init__.py

0001_initial.py

0001_initial.py

0002_delete_note.py

0002_delete_note.py

0003_initial.py

0003_initial.py

__init__.py

__init__.py

admin.py

admin.py

apps.py

apps.py

models.py

models.py

tests.py

tests.py

urls.py

urls.py

views.py

views.py

folder

notes

__init__.py

__init__.py

asgi.py

asgi.py

settings.py

settings.py

urls.py

urls.py

wsgi.py

wsgi.py

manage.py

manage.py

requirements.txt

requirements.txt

Everything was clear?

Section 4. Chapter 3
course content

Course Content

Django: Build Your First Website

Update and Delete OperationsUpdate and Delete Operations

In this chapter, we'll begin updating and deleting records from the database.

Deleting Records

AppFolder \ views.py
  1. all_notes = Note.objects.all(): Retrieves all Note objects from the database;
  2. for i in all_notes:: Iterates through all Note objects in the QuerySet;
  3. i.delete(): Deletes each Note object from the database;
  4. return HttpResponse('Notes deleted'): Returns an HTTP response with the text message "Notes deleted".

Updating Records

AppFolder \ views.py
  1. all_notes = Note.objects.all(): Retrieves all Note objects from the database;
  2. for note in all_notes:: Iterates through all Note objects in the QuerySet;
  3. note.title = 'LOL': Sets a new value for the title field of each Note object;
  4. note.save(): Saves the changes to the database;
  5. return HttpResponse('Notes updated'): Returns an HTTP response with the text message "Notes updated".

Please note that in update_content, we are changing the title field of each Note object to the value 'LOL'. You can adapt this code according to your specific requirements.

arrowfolder
__init__.py

__init__.py

folder/
folder

notes

folder

my_notes

folder

migrations

__init__.py

__init__.py

0001_initial.py

0001_initial.py

0002_delete_note.py

0002_delete_note.py

0003_initial.py

0003_initial.py

__init__.py

__init__.py

admin.py

admin.py

apps.py

apps.py

models.py

models.py

tests.py

tests.py

urls.py

urls.py

views.py

views.py

folder

notes

__init__.py

__init__.py

asgi.py

asgi.py

settings.py

settings.py

urls.py

urls.py

wsgi.py

wsgi.py

manage.py

manage.py

requirements.txt

requirements.txt

Everything was clear?

Section 4. Chapter 3
some-alt