程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

【Django】客制化日志來保存被修改的信息--20220803

編輯:Python

效果:打印日志和保存日志文件

2022-08-03 15:12:28.728130 season modified the data of {
'project_name': 'test234321', 'project_owner_id': 'Z18093014', 'project_owner_name': '黃雪玲1238', 'project_dept': 'MZH700', 'project_plan_start_date': datetime.date(2022, 8, 1), 'project_plan_end_date': datetime.date(2022, 8, 1), 'project_actual_start_date': datetime.date(2022, 8, 3), 'project_actual_end_date': datetime.date(2022, 8, 3), 'project_status': '2-ongoing', 'project_item1': '訓練委員會提前溝通方向', 'project_item1_status': '1-todo', 'project_item1_duedate': datetime.date(2022, 8, 1), 'project_item1_remark': '(NEW)'}

代碼:

views.py

# 拓展project模型
class projectAdmin(admin.ModelAdmin):
省略
def save_model(self, request, obj, form, change):
if change:
# print('self',self)
# print('request',request)
# print('obj',obj)
# print('form',form)
# print("form.cleaned_data['project_name']",form.cleaned_data['project_name'])
# print('change',change)
# print("form.cleaned_data['project_name']",form.cleaned_data['project_name'])
row_obj = project.objects.get(pk=obj.pid)
# print('row_obj',row_obj.project_name)
change_dict = {
}
if form.cleaned_data['project_name'] != row_obj.project_name or\
form.cleaned_data['project_owner_id'] != row_obj.project_owner_id or\
form.cleaned_data['project_owner_name'] != row_obj.project_owner_name or\
form.cleaned_data['project_dept'] != row_obj.project_dept or\
form.cleaned_data['project_plan_start_date'] != row_obj.project_plan_start_date or\
form.cleaned_data['project_plan_end_date'] != row_obj.project_plan_end_date or\
form.cleaned_data['project_status'] != row_obj.project_status :
process_dict = {
'project_name':form.cleaned_data['project_name'],
'project_owner_id':form.cleaned_data['project_owner_id'],
'project_owner_name':form.cleaned_data['project_owner_name'],
'project_dept':form.cleaned_data['project_dept'],
'project_plan_start_date':form.cleaned_data['project_plan_start_date'],
'project_plan_end_date':form.cleaned_data['project_plan_end_date'],
'project_actual_start_date':form.cleaned_data['project_actual_start_date'],
'project_actual_end_date':form.cleaned_data['project_actual_end_date'],
'project_status':form.cleaned_data['project_status'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item1'] != row_obj.project_item1 or \
form.cleaned_data['project_item1_status'] != row_obj.project_item1_status or \
form.cleaned_data['project_item1_duedate'] != row_obj.project_item1_duedate or \
form.cleaned_data['project_item1_remark'] != row_obj.project_item1_remark:
process_dict = {
'project_item1':form.cleaned_data['project_item1'],
'project_item1_status':form.cleaned_data['project_item1_status'],
'project_item1_duedate':form.cleaned_data['project_item1_duedate'],
'project_item1_remark':form.cleaned_data['project_item1_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item2'] != row_obj.project_item2 or \
form.cleaned_data['project_item2_status'] != row_obj.project_item2_status or \
form.cleaned_data['project_item2_duedate'] != row_obj.project_item2_duedate or \
form.cleaned_data['project_item2_remark'] != row_obj.project_item2_remark:
process_dict = {
'project_item2':form.cleaned_data['project_item2'],
'project_item2_status':form.cleaned_data['project_item2_status'],
'project_item2_duedate':form.cleaned_data['project_item2_duedate'],
'project_item2_remark':form.cleaned_data['project_item2_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item3'] != row_obj.project_item3 or \
form.cleaned_data['project_item3_status'] != row_obj.project_item3_status or \
form.cleaned_data['project_item3_duedate'] != row_obj.project_item3_duedate or \
form.cleaned_data['project_item3_remark'] != row_obj.project_item3_remark:
process_dict = {
'project_item3':form.cleaned_data['project_item3'],
'project_item3_status':form.cleaned_data['project_item3_status'],
'project_item3_duedate':form.cleaned_data['project_item3_duedate'],
'project_item3_remark':form.cleaned_data['project_item3_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item4'] != row_obj.project_item4 or \
form.cleaned_data['project_item4_status'] != row_obj.project_item4_status or \
form.cleaned_data['project_item4_duedate'] != row_obj.project_item4_duedate or \
form.cleaned_data['project_item4_remark'] != row_obj.project_item4_remark:
process_dict = {
'project_item4':form.cleaned_data['project_item4'],
'project_item4_status':form.cleaned_data['project_item4_status'],
'project_item4_duedate':form.cleaned_data['project_item4_duedate'],
'project_item4_remark':form.cleaned_data['project_item4_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item5'] != row_obj.project_item5 or \
form.cleaned_data['project_item5_status'] != row_obj.project_item5_status or \
form.cleaned_data['project_item5_duedate'] != row_obj.project_item5_duedate or \
form.cleaned_data['project_item5_remark'] != row_obj.project_item5_remark:
process_dict = {
'project_item5':form.cleaned_data['project_item5'],
'project_item5_status':form.cleaned_data['project_item5_status'],
'project_item5_duedate':form.cleaned_data['project_item5_duedate'],
'project_item5_remark':form.cleaned_data['project_item5_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item6'] != row_obj.project_item6 or \
form.cleaned_data['project_item6_status'] != row_obj.project_item6_status or \
form.cleaned_data['project_item6_duedate'] != row_obj.project_item6_duedate or \
form.cleaned_data['project_item6_remark'] != row_obj.project_item6_remark:
process_dict = {
'project_item6':form.cleaned_data['project_item6'],
'project_item6_status':form.cleaned_data['project_item6_status'],
'project_item6_duedate':form.cleaned_data['project_item6_duedate'],
'project_item6_remark':form.cleaned_data['project_item6_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item7'] != row_obj.project_item7 or \
form.cleaned_data['project_item7_status'] != row_obj.project_item7_status or \
form.cleaned_data['project_item7_duedate'] != row_obj.project_item7_duedate or \
form.cleaned_data['project_item7_remark'] != row_obj.project_item7_remark:
process_dict = {
'project_item7':form.cleaned_data['project_item7'],
'project_item7_status':form.cleaned_data['project_item7_status'],
'project_item7_duedate':form.cleaned_data['project_item7_duedate'],
'project_item7_remark':form.cleaned_data['project_item7_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item8'] != row_obj.project_item8 or \
form.cleaned_data['project_item8_status'] != row_obj.project_item8_status or \
form.cleaned_data['project_item8_duedate'] != row_obj.project_item8_duedate or \
form.cleaned_data['project_item8_remark'] != row_obj.project_item8_remark:
process_dict = {
'project_item8':form.cleaned_data['project_item8'],
'project_item8_status':form.cleaned_data['project_item8_status'],
'project_item8_duedate':form.cleaned_data['project_item8_duedate'],
'project_item8_remark':form.cleaned_data['project_item8_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item9'] != row_obj.project_item9 or \
form.cleaned_data['project_item9_status'] != row_obj.project_item9_status or \
form.cleaned_data['project_item9_duedate'] != row_obj.project_item9_duedate or \
form.cleaned_data['project_item9_remark'] != row_obj.project_item9_remark:
process_dict = {
'project_item9':form.cleaned_data['project_item9'],
'project_item9_status':form.cleaned_data['project_item9_status'],
'project_item9_duedate':form.cleaned_data['project_item9_duedate'],
'project_item9_remark':form.cleaned_data['project_item9_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item10'] != row_obj.project_item10 or \
form.cleaned_data['project_item10_status'] != row_obj.project_item10_status or \
form.cleaned_data['project_item10_duedate'] != row_obj.project_item10_duedate or \
form.cleaned_data['project_item10_remark'] != row_obj.project_item10_remark:
process_dict = {
'project_item10':form.cleaned_data['project_item10'],
'project_item10_status':form.cleaned_data['project_item10_status'],
'project_item10_duedate':form.cleaned_data['project_item10_duedate'],
'project_item10_remark':form.cleaned_data['project_item10_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item11'] != row_obj.project_item11 or \
form.cleaned_data['project_item11_status'] != row_obj.project_item11_status or \
form.cleaned_data['project_item11_duedate'] != row_obj.project_item11_duedate or \
form.cleaned_data['project_item11_remark'] != row_obj.project_item11_remark:
process_dict = {
'project_item11':form.cleaned_data['project_item11'],
'project_item11_status':form.cleaned_data['project_item11_status'],
'project_item11_duedate':form.cleaned_data['project_item11_duedate'],
'project_item11_remark':form.cleaned_data['project_item11_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item12'] != row_obj.project_item12 or \
form.cleaned_data['project_item12_status'] != row_obj.project_item12_status or \
form.cleaned_data['project_item12_duedate'] != row_obj.project_item12_duedate or \
form.cleaned_data['project_item12_remark'] != row_obj.project_item12_remark:
process_dict = {
'project_item12':form.cleaned_data['project_item12'],
'project_item12_status':form.cleaned_data['project_item12_status'],
'project_item12_duedate':form.cleaned_data['project_item12_duedate'],
'project_item12_remark':form.cleaned_data['project_item12_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item13'] != row_obj.project_item13 or \
form.cleaned_data['project_item13_status'] != row_obj.project_item13_status or \
form.cleaned_data['project_item13_duedate'] != row_obj.project_item13_duedate or \
form.cleaned_data['project_item13_remark'] != row_obj.project_item13_remark:
process_dict = {
'project_item13':form.cleaned_data['project_item13'],
'project_item13_status':form.cleaned_data['project_item13_status'],
'project_item13_duedate':form.cleaned_data['project_item13_duedate'],
'project_item13_remark':form.cleaned_data['project_item13_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item14'] != row_obj.project_item14 or \
form.cleaned_data['project_item14_status'] != row_obj.project_item14_status or \
form.cleaned_data['project_item14_duedate'] != row_obj.project_item14_duedate or \
form.cleaned_data['project_item14_remark'] != row_obj.project_item14_remark:
process_dict = {
'project_item14':form.cleaned_data['project_item14'],
'project_item14_status':form.cleaned_data['project_item14_status'],
'project_item14_duedate':form.cleaned_data['project_item14_duedate'],
'project_item14_remark':form.cleaned_data['project_item14_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item15'] != row_obj.project_item15 or \
form.cleaned_data['project_item15_status'] != row_obj.project_item15_status or \
form.cleaned_data['project_item15_duedate'] != row_obj.project_item15_duedate or \
form.cleaned_data['project_item15_remark'] != row_obj.project_item15_remark:
process_dict = {
'project_item15':form.cleaned_data['project_item15'],
'project_item15_status':form.cleaned_data['project_item15_status'],
'project_item15_duedate':form.cleaned_data['project_item15_duedate'],
'project_item15_remark':form.cleaned_data['project_item15_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item16'] != row_obj.project_item16 or \
form.cleaned_data['project_item16_status'] != row_obj.project_item16_status or \
form.cleaned_data['project_item16_duedate'] != row_obj.project_item16_duedate or \
form.cleaned_data['project_item16_remark'] != row_obj.project_item16_remark:
process_dict = {
'project_item16':form.cleaned_data['project_item16'],
'project_item16_status':form.cleaned_data['project_item16_status'],
'project_item16_duedate':form.cleaned_data['project_item16_duedate'],
'project_item16_remark':form.cleaned_data['project_item16_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item17'] != row_obj.project_item17 or \
form.cleaned_data['project_item17_status'] != row_obj.project_item17_status or \
form.cleaned_data['project_item17_duedate'] != row_obj.project_item17_duedate or \
form.cleaned_data['project_item17_remark'] != row_obj.project_item17_remark:
process_dict = {
'project_item17':form.cleaned_data['project_item17'],
'project_item17_status':form.cleaned_data['project_item17_status'],
'project_item17_duedate':form.cleaned_data['project_item17_duedate'],
'project_item17_remark':form.cleaned_data['project_item17_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item18'] != row_obj.project_item18 or \
form.cleaned_data['project_item18_status'] != row_obj.project_item18_status or \
form.cleaned_data['project_item18_duedate'] != row_obj.project_item18_duedate or \
form.cleaned_data['project_item18_remark'] != row_obj.project_item18_remark:
process_dict = {
'project_item18':form.cleaned_data['project_item18'],
'project_item18_status':form.cleaned_data['project_item18_status'],
'project_item18_duedate':form.cleaned_data['project_item18_duedate'],
'project_item18_remark':form.cleaned_data['project_item18_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item19'] != row_obj.project_item19 or \
form.cleaned_data['project_item19_status'] != row_obj.project_item19_status or \
form.cleaned_data['project_item19_duedate'] != row_obj.project_item19_duedate or \
form.cleaned_data['project_item19_remark'] != row_obj.project_item19_remark:
process_dict = {
'project_item19':form.cleaned_data['project_item19'],
'project_item19_status':form.cleaned_data['project_item19_status'],
'project_item19_duedate':form.cleaned_data['project_item19_duedate'],
'project_item19_remark':form.cleaned_data['project_item19_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item20'] != row_obj.project_item20 or \
form.cleaned_data['project_item20_status'] != row_obj.project_item20_status or \
form.cleaned_data['project_item20_duedate'] != row_obj.project_item20_duedate or \
form.cleaned_data['project_item20_remark'] != row_obj.project_item20_remark:
process_dict = {
'project_item20':form.cleaned_data['project_item20'],
'project_item20_status':form.cleaned_data['project_item20_status'],
'project_item20_duedate':form.cleaned_data['project_item20_duedate'],
'project_item20_remark':form.cleaned_data['project_item20_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item21'] != row_obj.project_item21 or \
form.cleaned_data['project_item21_status'] != row_obj.project_item21_status or \
form.cleaned_data['project_item21_duedate'] != row_obj.project_item21_duedate or \
form.cleaned_data['project_item21_remark'] != row_obj.project_item21_remark:
process_dict = {
'project_item21':form.cleaned_data['project_item21'],
'project_item21_status':form.cleaned_data['project_item21_status'],
'project_item21_duedate':form.cleaned_data['project_item21_duedate'],
'project_item21_remark':form.cleaned_data['project_item21_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item22'] != row_obj.project_item22 or \
form.cleaned_data['project_item22_status'] != row_obj.project_item22_status or \
form.cleaned_data['project_item22_duedate'] != row_obj.project_item22_duedate or \
form.cleaned_data['project_item22_remark'] != row_obj.project_item22_remark:
process_dict = {
'project_item22':form.cleaned_data['project_item22'],
'project_item22_status':form.cleaned_data['project_item22_status'],
'project_item22_duedate':form.cleaned_data['project_item22_duedate'],
'project_item22_remark':form.cleaned_data['project_item22_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item23'] != row_obj.project_item23 or \
form.cleaned_data['project_item23_status'] != row_obj.project_item23_status or \
form.cleaned_data['project_item23_duedate'] != row_obj.project_item23_duedate or \
form.cleaned_data['project_item23_remark'] != row_obj.project_item23_remark:
process_dict = {
'project_item23':form.cleaned_data['project_item23'],
'project_item23_status':form.cleaned_data['project_item23_status'],
'project_item23_duedate':form.cleaned_data['project_item23_duedate'],
'project_item23_remark':form.cleaned_data['project_item23_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item24'] != row_obj.project_item24 or \
form.cleaned_data['project_item24_status'] != row_obj.project_item24_status or \
form.cleaned_data['project_item24_duedate'] != row_obj.project_item24_duedate or \
form.cleaned_data['project_item24_remark'] != row_obj.project_item24_remark:
process_dict = {
'project_item24':form.cleaned_data['project_item24'],
'project_item24_status':form.cleaned_data['project_item24_status'],
'project_item24_duedate':form.cleaned_data['project_item24_duedate'],
'project_item24_remark':form.cleaned_data['project_item24_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item25'] != row_obj.project_item25 or \
form.cleaned_data['project_item25_status'] != row_obj.project_item25_status or \
form.cleaned_data['project_item25_duedate'] != row_obj.project_item25_duedate or \
form.cleaned_data['project_item25_remark'] != row_obj.project_item25_remark:
process_dict = {
'project_item25':form.cleaned_data['project_item25'],
'project_item25_status':form.cleaned_data['project_item25_status'],
'project_item25_duedate':form.cleaned_data['project_item25_duedate'],
'project_item25_remark':form.cleaned_data['project_item25_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item26'] != row_obj.project_item26 or \
form.cleaned_data['project_item26_status'] != row_obj.project_item26_status or \
form.cleaned_data['project_item26_duedate'] != row_obj.project_item26_duedate or \
form.cleaned_data['project_item26_remark'] != row_obj.project_item26_remark:
process_dict = {
'project_item26':form.cleaned_data['project_item26'],
'project_item26_status':form.cleaned_data['project_item26_status'],
'project_item26_duedate':form.cleaned_data['project_item26_duedate'],
'project_item26_remark':form.cleaned_data['project_item26_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item27'] != row_obj.project_item27 or \
form.cleaned_data['project_item27_status'] != row_obj.project_item27_status or \
form.cleaned_data['project_item27_duedate'] != row_obj.project_item27_duedate or \
form.cleaned_data['project_item27_remark'] != row_obj.project_item27_remark:
process_dict = {
'project_item27':form.cleaned_data['project_item27'],
'project_item27_status':form.cleaned_data['project_item27_status'],
'project_item27_duedate':form.cleaned_data['project_item27_duedate'],
'project_item27_remark':form.cleaned_data['project_item27_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item28'] != row_obj.project_item28 or \
form.cleaned_data['project_item28_status'] != row_obj.project_item28_status or \
form.cleaned_data['project_item28_duedate'] != row_obj.project_item28_duedate or \
form.cleaned_data['project_item28_remark'] != row_obj.project_item28_remark:
process_dict = {
'project_item28':form.cleaned_data['project_item28'],
'project_item28_status':form.cleaned_data['project_item28_status'],
'project_item28_duedate':form.cleaned_data['project_item28_duedate'],
'project_item28_remark':form.cleaned_data['project_item28_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item29'] != row_obj.project_item29 or \
form.cleaned_data['project_item29_status'] != row_obj.project_item29_status or \
form.cleaned_data['project_item29_duedate'] != row_obj.project_item29_duedate or \
form.cleaned_data['project_item29_remark'] != row_obj.project_item29_remark:
process_dict = {
'project_item29':form.cleaned_data['project_item29'],
'project_item29_status':form.cleaned_data['project_item29_status'],
'project_item29_duedate':form.cleaned_data['project_item29_duedate'],
'project_item29_remark':form.cleaned_data['project_item29_remark'],
}
change_dict.update(process_dict)
if form.cleaned_data['project_item30'] != row_obj.project_item30 or \
form.cleaned_data['project_item30_status'] != row_obj.project_item30_status or \
form.cleaned_data['project_item30_duedate'] != row_obj.project_item30_duedate or \
form.cleaned_data['project_item30_remark'] != row_obj.project_item30_remark:
process_dict = {
'project_item30':form.cleaned_data['project_item30'],
'project_item30_status':form.cleaned_data['project_item30_status'],
'project_item30_duedate':form.cleaned_data['project_item30_duedate'],
'project_item30_remark':form.cleaned_data['project_item30_remark'],
}
change_dict.update(process_dict)
logger.info("%s %s modified the data of %s " % (datetime.now(),request.user.username, change_dict))
super().save_model(request, obj, form, change)

settings.py

LOGGING = {

'version': 1,
'disable_existing_loggers': False,
'formatters': {

'simple': {
 # exact format is not important, this is the minimum information
'format': '%(asctime)s %(name)-12s %(lineno)d %(levelname)-8s %(message)s',
},
},
'handlers': {

'console': {

'class': 'logging.StreamHandler',
},
# 'mail_admins': { # Add Handler for mail_admins for `warning` and above
# 'level': 'ERROR',
# 'class': 'django.utils.log.AdminEmailHandler',
# },
'file': {

'class': 'logging.FileHandler',
'filename': os.path.join(LOG_DIR, 'admin.log'),
},
},
'root': {

'handlers': ['console', 'file'],
'level': 'INFO',
},
}

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved