Django_admin data management background

Table of contents

1. Basic operation

2. Custom background operation data behavior

Source code and other data acquisition methods


The admin data management background is a built-in background management interface of django, which can view the data structure of registered model classes, as well as add, delete and modify data.

1. Basic operation

1.1 Check whether the urls.py in the project directory has the following configuration

1.2 Create django's admin background management account

python manage.py createsuperuser

1.3 Register the model class in the admin.py file in the corresponding directory of the app

1.4 Enter 127.0.0.1/admin in the browser, enter the account number and password created above to enter the admin management background, after logging in, you can view the registered model classes

 GoodsType model class

1.5 Click on the corresponding model class to add, delete, modify and check the registered model class

2. Custom background operation data behavior

2.1 Rewrite the admin.ModelAdmin class

class BaseModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        """新增或更新表中数据时自动调用"""
        # 1.继承原方法操作数据行为
        super(BaseModelAdmin, self).save_model(request, obj, form, change)

        # 2.自定义操作数据行为
        print("数据发生了变更")
    

    def delete_model(self, request, obj):
        """删除表中数据时自动调用"""
        # 1.继承原方法操作数据行为
        super(BaseModelAdmin, self).delete_model(request, obj)

        # 2.自定义操作数据行为
        print("数据被删除了")

2.2 Re-register the model class

# 模型类关联重写的BaseModelAdmin类
admin.site.register(GoodsType, BaseModelAdmin)

2.3 Operate the model from the admin background

 You can check the workbench log to confirm that the default behavior has been modified


Source code and other data acquisition methods

 Friends who want to get the source code, please like + comment + favorite , triple!

After three times in a row , I will send you private messages one by one in the comment area~

Guess you like

Origin blog.csdn.net/GDYY3721/article/details/131660604