删除django中的多个对象 [英] Delete multiple objects in django

查看:20
本文介绍了删除django中的多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用网页从 django 的数据库中选择要删除的几个对象.没有可供选择的类别,所以我不能像那样从所有类别中删除.我是否必须实现我自己的删除表单并在 django 中处理它,或者 django 是否已经有办法做到这一点?正如它在管理界面中实现的那样.

I need to select several objects to be deleted from my database in django using a webpage. There is no category to select from so I can't delete from all of them like that. Do I have to implement my own delete form and process it in django or does django have a way to already do this? As its implemented in the admin interface.

推荐答案

您可以删除任何您喜欢的 QuerySet.例如,删除所有具有某些 Post 模型的博客帖子

You can delete any QuerySet you'd like. For example, to delete all blog posts with some Post model

Post.objects.all().delete()

并删除任何发布日期为未来的帖子

and to delete any Post with a future publication date

Post.objects.filter(pub_date__gt=datetime.now()).delete()

但是,您确实需要想出一种方法来缩小您的 QuerySet.如果您只想删除特定对象的视图,请查看 删除通用视图.

You do, however, need to come up with a way to narrow down your QuerySet. If you just want a view to delete a particular object, look into the delete generic view.

抱歉造成误会.我认为答案介于两者之间.要实现你自己的,结合 ModelForms 和 通用视图.否则,请查看提供类似功能的 3rd 方应用程序.在相关问题中,建议是django-filter.

Sorry for the misunderstanding. I think the answer is somewhere between. To implement your own, combine ModelForms and generic views. Otherwise, look into 3rd party apps that provide similar functionality. In a related question, the recommendation was django-filter.

这篇关于删除django中的多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆