Saturday, 17 July 2021

*Episode 22* PYTHON (Create and Delete objects of Django)

 


Creating and deleting objects


This episode covers :-


 1) How to delete Flower objects with a custom view.

 2) How to use the Python interactive interpreter to manipulate objects and interact with Django.


  •  Setup :-

Terminal

cp -fr 23-Forms-Customization 24-Object-Manipulation

cd 24-Object-Manipulation

source ../venv/bin/activate


  • Adding the delete path :-

    Edit mysite urls.py file and add the delete path:

mysite/urls.py

urlpatterns = [

path('admin/', admin.site.urls),

path('', myapp_views.index, name='index'),

path('flower/create/', myapp_views.create, name='create\

'),

path('flower/edit/<int:pk>/', myapp_views.edit, name='e\

dit'),

path('flower/delete/<int:pk>/', myapp_views.delete, nam\

e='delete'), # < here

]


  • Adding the delete view :-

    We don’t necessary need a form to delete items. You could simple capture the pk from the URL and do the deletion logic in a view.


     Edit myapp views.py file and add the delete view:

myapp/views.py

def index(request):

...

def create(request):

...

def edit(request, pk=None):

def delete(request, pk=None): # < here

flower = get_object_or_404(Flower, pk=pk)

flower.delete()

return render(request, 'myapp/index.html')


  •  Updating the delete link :-

     Edit the myapp index.html template and update the delete link:

myapp/templates/myapp/index.html

<div class="card-body">

...

<a href="{% url 'edit' pk=flower.pk %}" class="card-link">\

Edit</a>

<a href="{% url 'delete' pk=flower.pk %}" class="card-link\

">Delete</a> # < here

</div>


     You can now use the delete links on the homepage to erase items.


  •  In Details :-

     Make sure you have activated the virtual environment and open the Python interactive interpreter:

Interactive interpreter

python manage.py shell

>>> from myapp.models import Flower

>>> flower = Flower(title="Agathis")

>>> flower

<Flower: Agathis>

>>> flower.save()


     python manage.py shell starts the interactive session.


     Flower model can be instantiated like any class. Flower(title="Agathis") creates a new Flower object with the title “Agathis”.


     Flower.save() stores it in the database. Visit homepage to confirm that it was actually created:



     In the myapp views.py file we use flower.delete() method to delete the object from the database:

delete() method erases the object from the database

flower = get_object_or_404(Flower, pk=pk)

flower.delete()


     You can do the same thing in the interactive interpreter:

Interactive interpreter

>>> flower.delete()

(1, {'myapp.Flower': 1})

>>>


     flower.delete() returns how many objects were deleted and how many deletions were executed by object type: {'myapp.Flower': 1}. We deleted 1 object of the type Flower.


     You can get and update an object like this:

Interactive interpreter

>>> flower = Flower.objects.get(pk=1)

>>> flower

<Flower: Amelanchier alnifolia...>

>>> flower.title = "UPDATED"

>>> flower.save()

>>> flower

<Flower: UPDATED>

>>>


Summary :-


  •  You can use the Python interactive interpreter to run Python code and interact with your Django apps.
  •  object = Class() instantiates a Class object.
  •  object.save() saves the object to the database or updates it.
  •  object.delete() deletes the object from the database.

👈Episode 21(P).                                                                            Episode 23(P)👉
Share This Post
PRINT THIS POST

No comments:

Post a Comment

If you have any doubts. Please let me know.

Featured post

*Episode 1* MCQ for Govt. Job/ Private Job/ MNCs

  Topic:- One Word Substitution 1) Especially skilled in storytelling  Answer:- Raconteur 2) Fear of loneliness Answer:- Eremophobia  3) Usa...