Skip to content

Storage Update

In this video, we are going to learn about generic Update View.

Generic UpdateView is a class-based view that provides a pre-built implementation for updating existing objects in a database. It simplifies the process of handling form submission and object modification in Django.

In this video, we are going to write an Update View api for Storage model. Resources:

View

At first, open your views.py file of center and then create a Storage Update Class.

class StorageUpdate(generic.UpdateView):

This class inherits generic.UpdateView.

In this class, at first we need to specify the model for which we have to perform Update operation.

    model = Storage

Now, we need to specify the form class for this UpdateView. We will use the same StorageForm which we used in create storage view.

    form_class = StorageForm

After that, we need to provide the center_id to the form. Because the StorageForm takes center_id in its constructor. So, to provide additional data to the form, we will override get_form_kwargs method.

def get_form_kwargs(self):
    # At first grab the kwargs from the super() method
    kwargs = super().get_form_kwargs()
    # Now add the center_id to this kwargs.
    kwargs["center_id"] = self.get_object().center.id
    return kwargs

Template

After specify the form_class, we need to specify the template_name for this UpdateView.

template_name = "storage/storage-update.html"

Now, create a template named "storage-update.html". I have attached a file named "storage-update.html" in the resources section. Download it and copy its content to this HTML file.

{% extends 'mysite/base.html' %}

{% block title %} 
    <title>Update Storage</title>
{% endblock title %}

{% block content %}
<div class="m-2 p-2">
  <h4 class="mb-3">Fill the following details to update this storage</h4>
  {% include "components/form.html" with form_name="Update Storage" %}
</div>
{% endblock content %}

URL

Till now, we have specified the model name, form_class and template name. At last, we need to specify the success url. Because once the update process get completed, django needs to redirect the user to cetain page. As of now, we will redirect user to storage list page once the user completes the update process.

So, to specify the success url, we will override the get_success_url method.

def get_success_url(self):
    # Now, use reverse method to return the storage list page
    return reverse("center:storage-list", kwargs={"center_id": self.get_object().center.id})

Finally, lets add a path to this update view in the urlpatterns.

path("storage/update/<int:pk>/", views.StorageUpdate.as_view(), name="storage-update"),

Using this primary key, django will know the instance of storage which you want to update.

At last, we need to use this url in the storage-detail.html file.

<a href="{% url 'center:storage-update' object.id %}">
    <i class="fas fa-pen"></i> Update Storage
</a>

[Run the development server and see the changes.]

In this way, we have learnt how to perform update operation using generic UpdateView for any given model.