Conteúdo do Curso
Django: Build Your First Website
Django: Build Your First Website
Update and Delete Operations
In this chapter, we'll begin updating and deleting records from the database.
Deleting Records
all_notes = Note.objects.all()
: Retrieves allNote
objects from the database;for i in all_notes:
: Iterates through allNote
objects in the QuerySet;i.delete()
: Deletes eachNote
object from the database;return HttpResponse('Notes deleted')
: Returns an HTTP response with the text message "Notes deleted".
Updating Records
all_notes = Note.objects.all()
: Retrieves allNote
objects from the database;for note in all_notes:
: Iterates through allNote
objects in the QuerySet;note.title = 'LOL'
: Sets a new value for thetitle
field of eachNote
object;note.save()
: Saves the changes to the database;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.
__init__.py
notes
my_notes
migrations
__init__.py
0001_initial.py
0002_delete_note.py
0003_initial.py
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
notes
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
manage.py
requirements.txt
Obrigado pelo seu feedback!