Django operates MySQL database through admin, and the audit function is added to store records

Empire my love 2022-05-14 14:54:19 阅读数:829

djangooperatesmysqldatabaseadmin

1. establish MySQL database moderate surface and  moderate_log( Storage record ) surface

 

2.Django mapping

 

 

(1)inspectdb Create a table

 

(2)  Copy the contents of the red box , Paste the models.py file

You can add the following code or not , If you add it, you need to map

verbose_name = "Moderate" # Display name of the home page list
verbose_name_plural = verbose_name # Display name of list page and detail page 

 

 

(3) Mapping the

3. stay admin.py file Write code ( Core code )

from django.contrib import admin
# from Your own project .models import *
from book_MS.models import *
# date You can choose to add or not
# from django.utils import timezone
@admin.register(Moderate)
# admin.site.register( Table to write ) And @admin.register( Table to write ) The function is the same
class ModerateAdmin(admin.ModelAdmin):
"""
*MySQL database moderate surface :id, name , Content , Whether to pass , Has it been approved
'id', 'name', 'incident', 'status', 'check'
"""
# listdisplay Set the fields to display in the list (id The fields are Django Default primary key of the model )
list_display = ('name', 'incident', 'status', )
# list_per_page Set how many records to display per page , The default is 10 strip
list_per_page = 10
# list_filter Filter the specified fields
list_filter = ('name', )
# modify admin page actions Information about
actions = ['mak_pub', 'mak_pub1']
# Pass the judgment
def mak_pub(self, request, queryset):
# Get the name of the current user
us = request.user
# Print passed data
for i in queryset.filter():
# print(i.id)
# establish str, If you need more time , Just add the following code
# str = '{} {} To change the Moderate Tabular id by {} Information about : Passed , Audit successful !'.format(timezone.now(), us, i.id)
str = '{} To change the Moderate Tabular id by {} Information about : Passed , Audit successful !'.format(us, i.id)
# Insert data into Log In the table
ModerateLog.objects.create(record=str)
# Update status and audit
rows_upb = queryset.update(status="1", check="1")
# If the number obtained is 1, Then execute the following code
if rows_upb == 1:
message_bit = "1 A video "
else:
message_bit = "%s A video " % rows_upb
# Through how much data , According to the admin On the page
self.message_user(request, "%s Have gone through ." % message_bit)
# change Action The content of is through
mak_pub.short_description = " adopt "
# Failed judgment
def mak_pub1(self, request, queryset):
# Get the current user
us = request.user
# Print failed data
for i in queryset.filter():
print(i)
# establish str
str = '{} To change the Moderate Tabular id by {} Information about : Not through , Audit successful !'.format(us, i.id)
# Insert data into Log In the table
ModerateLog.objects.create(record=str)
# Update status and audit
rows_upb = queryset.update(status="0", check="1")
# If the number obtained is 1, Then execute the following code
if rows_upb == 1:
message_bit = "1 A video "
else:
message_bit = "%s A video " % rows_upb
# Through how much data , According to the admin On the page
self.message_user(request, "%s Refuse to pass ." % message_bit)
# change Action The content of is through
mak_pub1.short_description = " Not through "
# Rewrite audited data , Super administrators will not pass
def get_queryset(self, request):
# Get all the data of the current table
qs = super().get_queryset(request)
# Determine whether there is no super administrator , If so, all ( Audited and unapproved ) Information about , Not to display unapproved information
if request.user.is_superuser:
return qs
return qs.filter(check=0)

4. Log in admin Background management

(1) Log in to super administrator first , Create administrator , Then give the administrator to view and modify moderate surface and  moderate_log( Storage record ) Table authority

(2) Remember to tick the employee status , Otherwise you can't log in  

 

 5. Log in to the administrator you just created

 6. Add data to test , To MySQL Manually enter several pieces of data into the database

 7. effect

(1) Click pass or fail , After execution, it will hide , Only super administrators can see , And it will record moderate_log( Storage record ) In the table

版权声明:本文为[Empire my love]所创,转载请带上原文链接,感谢。 https://javamana.com/2022/134/202205141446235358.html