CRM手記- 12 | Django
阿新 • • 發佈:2018-01-24
change 多對多 ken html 表數 刪除外鍵 comm 關聯 form
# 4.刪除 # 在修改記錄頁面左下角添加一個刪除按鈕,一個鏈接,跳轉至一個提示刪除內容的頁面,確認刪除; # 關聯刪除,刪除記錄同時也刪除外鍵的關聯表; # table_obj_common.html <div class="form-group"> <div class="col-sm-10"> <a href="{% table_obj_delete app_name model_name form_obj.instance.id %}">DELETE</a> </div> </div> #urls.py url(r‘^(\w+)/(\w+)/(\d+)/add/$‘, views.table_obj_delete, name="table_obj_delete"), # views.py @login_required def table_obj_delete(app_name,model_name,obj_id): """刪除記錄""" admin_class = site.enabled_admins[app_name][model_name] obj= admin_class.model.objects.get(id=obj_id) return render(request, ‘kingadmin/table_obj_delete.html‘,locals()) # table_obj_delete.html delete {{ obj }} <!-- obj代表當前這條記錄 --> # 在刪除頁面確認,後臺刪除該記錄; # >> a = models.CustomerInfo.objects.first() # >> a.delete() # 會不會刪除關聯的表數據?會;|跟蹤記錄表;# django-admin在刪除時顯示友好提示,列出所有關聯表; # 一層層遞歸所有外鍵,刪除; # table_obj_delete.html {% display_all_related_objs obj as all_related_obj_eles%} {{ all_related_obj_eles|safe }} # kingadmin_tag.py @register.simple_tag def display_all_related_objs(obj): """顯示要被刪除對象的所有關聯對象""" ele = "<ul>" ele += "<li>%s</i>"%obj # 1.forignkey | get_internal_type() for reversed_fk_obj in obj._meta.related_objects: related_table_name = reversed_fk_obj.name related_lookup_key = "%s_set"%related_table_name # >> getattr(a,‘customerinfo_set‘) # 獲取關聯表 # >> getattr(a,‘customerinfo_set‘).all() # 關聯表所有對象 related_objs = getattr(obj,related_lookup_key).all() # 反向查所有關聯數據 ele += "<li>%s</li><ul>"%related_table_name # 如果是many_to_many就不需要深入遞歸,打印影響的數據 if reversed_fk_obj.get_internal_type() == "ManyToManyField": for i in related_objs: ele += "<li><a href=‘/kingadmin/%s/%s/%s/change/‘>%s</a> 記錄中與%s相關數據將被刪除</li>" % (i._meta.app_label,i._meta.model_name,i.id,i, obj) # 課程相關 else: for i in related_objs: ele += "<li>%s</li>" % i ele += display_all_related_objs(i) # 調用自己,遞歸查找 ele += "<ul></li>" ele += "</ul>" return ele # >> a._meta.fields # 獲取所有字段 # >> a._meta.fields_map # 外鏈的表映射 # (‘student‘:<ManyToOneRel:crm.student>,...) # >> from crm import models # >> a = models.CustomerInfo.objects.all()[1] # >> a # <CustomerInfo: ‘大西瓜‘> # 學員表中的學生是外鍵關聯著客戶表的;一對多反向查詢(外鍵設置在student); # >> a.student_set.select_related() # 獲取當前查詢對象的所有外鍵字段對應表的查詢集:student # <QuerySet [<Student:‘大西瓜‘ >]> # 只要a._meta.fields_map遇到ManyToOneRel的,就反向去查找: # 要考慮自關聯,referer_from 轉介紹 # >> a._meta.fields_map # (...‘customerinfo‘:<ManyToOneRel:crm.student>...) # 映射中含有當前的表,忽略; # 綜合: # //////a._meta.fields可以獲得所有foreignkey # 不需要管,學生刪除不影響客戶表; # a._meta.related_obj 可以獲取所有反向關聯;而不使用a._meta.fields_map,因為有個臟數據需要判斷; # a._meta.many_to_many可以獲取多對多關系的數據; # 多層級遞歸刪除 # 課程-班級-上課記錄-學習記錄; # 課程被刪除,與之多對多關系的客戶信息不會被刪除,但是字段會收到影響; # 給展示記錄添加鏈接跳轉查看 ele += "<li><a href=‘/kingadmin/%s/%s/%s/change/‘>%s</a> 記錄中與%s相關數據將被刪除</li>" % (app_name,model_name,i.id,i, obj) {% display_all_related_objs i._meta.app_label,i._meta.model_name,i.id,i, obj as all_related_obj_eles%} # table_obj_delete.html | 點擊刪除所有關聯數據 <form method=‘post‘> {% csrf_token %} <input type=‘submit‘ class=‘btn btn-danger‘ value=‘確認刪除‘> <a>返回</a> </form> # views.py @login_required def table_obj_delete(app_name,model_name,obj_id): """刪除記錄""" admin_class = site.enabled_admins[app_name][model_name] obj = admin_class.model.objects.get(id=obj_id) if request.method == ‘POST‘: obj.delete() return redirect(‘/kingadmin/{app_name}/{model_name}/‘.format(app_name=app_name,model_name=model_name)) return render(request, ‘kingadmin/table_obj_delete.html‘,locals())
CRM手記- 12 | Django