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

查看:71
本文介绍了在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。例如,要删除所有具有某些帖子模型的博客帖子

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.

编辑:

对不起,造成误解。我认为答案介于两者之间。要实现自己的实现,请结合使用 ModelForm s和通用视图。否则,请研究提供类似功能的第三方应用程序。在相关问题中,建议为 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天全站免登陆