1、在table_objs_change.html文件加上删除按钮:
<div class="col-sm-2">
<button type="button" class="btn btn-danger">
<a href="{% url 'table_objs_delete' app_name table_name form_obj.instance.id %}">Delete</a>
</button>
</div>
2、在urls.py文件中加上url:
url(r'^(w+)/(w+)/(d+)/delete/$', views.table_objs_delete, name='table_objs_delete'),
3、新建table_objs_delete.html文件:
{% extends "kindadmin/table_index.html"%}
{% load tags %}
{% block container%}
{% display_obj_related obj %}
{% endblock%}
4、在view.py文件中返回table_objs_delete.html页面:
def table_objs_delete(request,app_name,table_name,obj_id):
"""
#删除数据
:param request:
:param app_name: crm
:param table_name: Costomer
:param obj_id: Costomer.id
:return:
"""
admin_class = kind_admin.enabled_admins[app_name][table_name]
obj = admin_class.model.objects.get(id=obj_id)
return render(request,"kindadmin/table_objs_delete.html",{"admin_class":admin_class,
"obj":obj})
5、在tags.py文件中处理返回页面的逻辑,表onetonone,manytomany的关系取值,利用递归去一层层的找(这个有点蒙)
@register.simple_tag
def recursive_related_objs_lookup(objs):
#model_name = objs[0]._meta.model_namme
#m = models.Customer.objects.get(id=20)
ul_ele = "<ul>"
for obj in objs:
li_ele = '''<li>%s:%s</li>'''%(obj._meta.verbose_name,obj.__str__().strip("<>"))
ul_ele +=li_ele
#many_to_many的对象
#print("------- obj._meta.local_many_to_many", obj._meta.local_many_to_many)
for m2m_field in obj._meta.local_many_to_many: #把所有跟这个对象直接关联的m2m字段取出来
sub_ul_ele = "<ul>"
m2m_field_obj = getattr(obj,m2m_field.name) #getattr(customer,'tags')
for o in m2m_field_obj.select_related(): #customer.tags.select_related()
li_ele ='''<li>%s:%s</li>''' %(m2m_field.verbose_name,o.__str__().strip("<>"))
sub_ul_ele+=li_ele
sub_ul_ele +="</ul>"
ul_ele +=sub_ul_ele #最后跟最外层的ul相拼接
#一对一的
#red =m._meta.related_objects
for related_obj in obj._meta.related_objects:
#red[1].__repr__()
if 'ManyToManyRel' in related_obj.__repr__():
#hasattr(m,red[0].get_accessor_name())
if hasattr(obj,related_obj.get_accessor_name()):
#getattr(m,red[0].get_accessor_name())
accessor_obj = getattr(obj,related_obj.get_accessor_name())
#上面的accessor_obj相当于customer.enrollment_set
if hasattr(accessor_obj,'select_related'): #select_related()==all()
target_objs = accessor_obj.select_related() # .filter(**filter_coditions)
# target_objs 相当于 customer.enrollment_set.all()
sub_ul_ele = "<ul style='color:red'>"
for o in target_objs:
li_ele = '''<li> %s:%s </li>''' %(o._.meta.verbose_name,o.__str__().strip("<>"))
sub_ul_ele+=li_ele
sub_ul_ele+="</ul>"
ul_ele+=sub_ul_ele
elif hasattr(obj,related_obj.get_accessor_name()): ## hassattr(customer,'enrollment_set')
accessor_obj = getattr(obj,related_obj.get_accessor_name())
# 上面accessor_obj 相当于 customer.enrollment_set
if hasattr(accessor_obj,'select_related'): #select_related() == all()
target_objs = accessor_obj.select_related() ##.filter(**filter_coditions)
# target_objs 相当于 customer.enrollment_set.all()
else:
print("one to one i guess:",accessor_obj)
target_objs = accessor_obj
if len(target_objs) >0:
nodes = recursive_related_objs_lookup(target_objs)
ul_ele += nodes
ul_ele+="</ul>"
return ul_ele
@register.simple_tag
def display_obj_related(obj):
objs = [obj,]
if objs:
model_class = objs[0]._meta.model #models.Customer
model_name = objs[0]._meta.model_name
return mark_safe(recursive_related_objs_lookup(objs))
在删除页面内容如下: