如何在Django中使用DeleteView显示相关项目?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Django中使用DeleteView显示相关项目?相关的知识,希望对你有一定的参考价值。

我正在做一个视图从模型中删除(使用Django的通用视图DeleteView)一个实例,但它从其他模型级联并删除实例:

url(r'^person/(?P<pk>d+)/delete/$', login_required(DeleteView.as_view(model=Person, success_url='/person/', template_name='delete.html')), name='person_delete'),

我想要做的是显示将被删除的相关项目列表,如管理界面所做的那样,如:

Are you sure you are going to delete Person NAMEOFTHEPERSON?
By deleting it, you are also going to delete:
CLASSNAME1: CLASSOBJECT1 ; CLASSNAME2: CLASSOBJECT2 ; CLASSNAME3: CLASSOBJECT3 ; etc
答案

您可以使用Django用于确定要在级联中删除的对象的Collector类。实例化它,然后在其上调用collect,传递您要删除的对象。它需要一个列表或查询集,所以如果你只有一个对象,只需放入一个列表:

from django.db.models.deletion import Collector

collector = Collector(using='default') # or specific database
collector.collect([some_instance])
for model, instance in collector.instances_with_model():
    # do something

instances_with_model返回一个生成器,因此您只能在循环的上下文中使用它。如果您更喜欢可以操作的实际数据结构,那么admin contrib包有一个名为CollectorNestedObjects子类,其工作方式相同,但有一个返回分层列表的nested方法:

from django.contrib.admin.utils import NestedObjects

collector = NestedObjects(using='default') # or specific database
collector.collect([some_instance])
to_delete = collector.nested()

更新:自Django 1.9以来,django.contrib.admin.util被重命名为django.contrib.admin.utils

另一答案

更新:从Django 1.9+ NestedObject应该从django.contrib.admin.utils导入

    from django.contrib.admin.utils import NestedObjects
另一答案

我使用来自管理员的get_deleted_objects()的缩减修改,并使用它在删除视图中的get_context中扩展我的上下文:

define somewhere

from django.contrib.admin.utils import NestedObjects
from django.utils.text import capfirst
from django.utils.encoding import force_text

def get_deleted_objects(objs): 
    collector = NestedObjects(using='default')
    collector.collect(objs)
    #
    def format_callback(obj):
        opts = obj._meta
        no_edit_link = '%s: %s' % (capfirst(opts.verbose_name),
                                   force_text(obj))
        return no_edit_link            
    #
    to_delete = collector.nested(format_callback)
    protected = [format_callback(obj) for obj in collector.protected]
    model_count = {model._meta.verbose_name_plural: len(objs) for model, objs in collector.model_objs.items()}
    #
    return to_delete, model_count, protected

then in your views

from somewhere import get_deleted_objects
#
class ExampleDelete(DeleteView):
    # ...
    def get_context_data(self, **kwargs):
        #
        context = super().get_context_data(**kwargs)
        #
        deletable_objects, model_count, protected = get_deleted_objects([self.object])
        #
        context['deletable_objects']=deletable_objects
        context['model_count']=dict(model_count).items()
        context['protected']=protected
        #
        return context

now you can use them in your template

<table>
  <tr>
    <th>Name</th>
    <th>Amount</th>
  </tr>
  {% for model_name, object_count in model_count %}
    <tr>
      <td>{{ model_name|capfirst }}</td>
      <td>{{ object_count }}</td>
    </tr>
  {% endfor %}
</table>
<p>
  <ul>
    {{ deletable_objects|unordered_list }}
  </ul>
</p>

Most is just copy/paste/edit/delete unwanted from django admin

以上是关于如何在Django中使用DeleteView显示相关项目?的主要内容,如果未能解决你的问题,请参考以下文章

如何使 ManyToMany 字段的值显示在 Django 通用 DeleteView 中

Django:使用通用 DeleteView 删除选中的项目?

如何在 Django 中为特定用户和特定项目使用 deleteview?

Django:我可以以相同的形式使用 CreateView 和 DeleteView 吗?

如何将额外的 URL 值传递给 Django 通用 DeleteView?

Django 基于类的 DeleteView 示例