Django Admin - Update Objects



Once a model is registered with Django’s Admin app, we can easily perform CRUD operations on the model. In the previous chapter, we learned to register the Employee model. The homepage of admin site shows it under the MYAPP section −

Django Update Objects 1

Display The List of Objects

To display the list of objects, click on the name −

Django Update Objects 2

Add / Update / Delete an Object

To add/update/delete an object from the model, the current user must have been granted the required permission. Note that the superuser is equipped with all the permissions. Here, we assume that you have logged into the admin site with the superuser credentials.

If you want to change the contents of any of the objects displayed in the above figure, just click on the corresponding row in the list.

Let us open the object on the top of the list.

Django Update Objects 3

Let us update the values of Empname and Salary fields as shown. Click on the SAVE button, so that the corresponding row of the backend table is also updated.

The list of objects reappears with the changes made being reflected.

Django Update Objects 4

Note that the admin interface lets you to add/update/delete an object from the model, but it is not possible to change the structure of the model from the admin interface.

To add/modify/delete any of the attributes of any model, you need to edit the class definition and perform migrations for the change to be propagated to the database table mapped to the object.

Advertisements