Django 简明教程

Django Admin - Update Objects

一旦模型向 Django 的 Admin 应用程序注册,我们就可以轻松地对该模型执行 CRUD 操作。在前一章节中,我们学习了注册 Employee model 。管理员网站的主页显示它在 MYAPP 部分下 −

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

要从模型中添加/更新/删除对象,当前用户必须获得所需的权限。请注意, superuser 具备所有权限。在这里,我们假设你已使用超级用户凭据登录管理员网站。

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

让我们更新 EmpnameSalary 字段的值,如所示。单击 SAVE 按钮,以便相应的后台表行也得到更新。

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.