1. 程式人生 > >kingadmin後臺(4)、刪除對象頁面開發

kingadmin後臺(4)、刪除對象頁面開發

change 需要 lds 反向 als bject ken attr 取出

刪除頁面展示:
技術分享圖片

1、路由配置:

url(r'^(\w+)/(\w+)/(\d+)/delete$', views.delete_obj, name='delete_obj'),

2、視圖函數:

def delete_obj(request, app_name, model_name, id):
    """根據id刪除對象"""
    admin_class = site.enable_admins[app_name][model_name]
    model_class = admin_class.model
    model_name=model_class._meta.model_name
    obj = model_class.objects.filter(id=int(id)).first()
    if request.method=='POST':
        obj.delete()
        return redirect('/kingadmin/%s/%s' % (app_name, model_class._meta.model_name))
    return render(request,'kingadmin/del_obj.html',locals())

3、前端頁面:

<div class="text">
    <h2>Are you sure?</h2>
    <p>Are you sure you want to delete the {{ model_name }} "{{ obj }}"?
        All of the following related items will be deleted:</p>
</div>

<div>
    <!--展示與要刪除的關聯字段相關聯的表記錄對象-->
    {% display_all_related_objs obj %}
</div>

<form method="post">
    <input type="submit" value="確定刪除" class="btn btn-danger">
    
    <a href="{% url 'change_obj' app_name model_name obj.id %}">
        <input type="button" value="返回" class="btn btn-info">
    </a>
    {% csrf_token %}
</form>

4、自定義標簽手動生成相關聯表記錄對象:

@register.simple_tag
def display_all_related_objs(obj):
    """顯示要被刪除對象的所有關聯對象"""

    ele = "<ul>"
    # 1、取出與當前對象有 外鍵 關聯的所有 model表的關聯對象,
        # 例如:<class 'django.db.models.fields.reverse_related.ManyToOneRel'>
    for reversed_fk_obj in obj._meta.related_objects:

        # 2、拼接查詢關鍵字,例如modelname__set
        related_table_name = reversed_fk_obj.name
        related_lookup_key = "%s_set" % related_table_name

        # 3、子結構開始
        ele += "<li>%s<ul> " % related_table_name

        # 4、判斷關聯表對象的類型:ManyToManyField、OneToOneField、ForeignKey

        if reversed_fk_obj.get_internal_type() == "ManyToManyField":  # 不需要深入查找
            related_objs = getattr(obj, related_lookup_key).all()  # 反向查所有關聯的數據
            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)

        elif reversed_fk_obj.get_internal_type() == "OneToOneField":  # 不需要深入查找
            related_obj = getattr(obj, related_table_name, '')  # 反向查所有關聯的數據
            if related_obj:
                ele += "<li><a href='/kingadmin/%s/%s/%s/change'>%s</a> 記錄裏與[%s]相關的的數據將被刪除</li>"                        % (related_obj._meta.app_label, related_obj._meta.model_name, related_obj.id, related_obj, obj)
        
        elif reversed_fk_obj.get_internal_type() == "ForeignKey":
            #  例如通過刪除客戶,那麽就會刪除 與此客戶相關的客戶跟蹤記錄 和 student_enroll信息
            related_objs = getattr(obj, related_lookup_key).all()  # 反向查所有關聯的數據
            for i in related_objs:
                ele += "<li><a href='/kingadmin/%s/%s/%s/change'>%s</a></li>" % (i._meta.app_label,
                                                                                 i._meta.model_name,
                                                                                 i.id, i)
                ele += display_all_related_objs(i)
        ele += "</ul></li>"

    ele += "</ul>"
    return mark_safe(ele)

5、刪除按鈕 和 返回按鈕:確定刪除

<form method="post">
    <input type="submit" value="確定刪除" class="btn btn-danger">
    
    <a href="{% url 'change_obj' app_name model_name obj.id %}">
        <input type="button" value="返回" class="btn btn-info">
    </a>
    {% csrf_token %}
</form>

kingadmin後臺(4)、刪除對象頁面開發