如何删除 Django 模型中的记录?
我想删除特定记录,例如:
delete from table_name where id = 1;
如何在 django 模型中执行此操作?
I want to delete a particular record like:
delete from table_name where id = 1;
How can I do this in a django model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
有几种方法:
直接删除:
从实例中删除:
There are a couple of ways:
To delete it directly:
To delete it from an instance:
如果具有指定主键的对象不存在,这将引发异常,因为它首先尝试检索指定的对象。
如果指定主键的对象不存在并且直接生成查询,则不会引发异常
this will raise exception if the object with specified primary key doesn't exist because at first it tries to retrieve the specified object.
this wont raise exception if the object with specified primary key doesn't exist and it directly produces the query
如果您想删除一个实例,请编写代码
如果您想删除所有实例,请编写代码
if you want to delete one instance then write the code
if you want to delete all instance then write the code
如果您想删除一件商品
例如如果您想删除心愿单中的所有商品
If you want to delete one item
If you want to delete all items in Wishlist for example
通过 wolph 扩展答案
请注意,您应该将 request 作为参数传递给视图中的删除函数。一个例子是这样的:
Extending the answer by wolph
Note that you should pass request as a parameter to your delete function in your views. An example would be like:
delete()
方法用于从数据库中删除模型实例。此方法会立即删除对象。该方法返回删除的对象的数量。示例:
删除一条记录:
由于 get 方法从查询集中返回单个对象,因此只会删除单个记录。如果提供的值不存在,这将引发错误。如果表中存在具有相同值的多条记录,那么它也会被删除抛出错误,因此好的做法是在使用 get 时使用单个唯一值。
根据条件删除多条记录:
对于基于条件的删除,在查询集上使用过滤方法,然后调用删除。
删除所有记录:
要从数据库表中删除所有模型实例/记录,您需要对所有实例调用删除方法
注意:代码可以单行编写为
Modelname.objects.all().delete(),但为了清楚理解,我使用了多行。
The
delete()
method is used to delete model instances from a database.This method immediately deletes the object. this method returns the number of object deleted.Example:
For deleting one record:
As get method returns a single object from queryset only single record will be deleted.If value supplied doesn't exist this will throw an error.If there are multilpe records in table for same value then also it will throw an error so good practice is to use a single unique value while using get.
For deleting multiple record according to a condition:
For condition based deletion filter method is used on queryset and then delete is called.
For deleting all records:
For deletion of all model instances/records from database table you need to call delete method on all
Note: code can be written in single line as
Modelname.objects.all().delete()
, but for clear understanding, I have used multiple lines.您还可以使用
get_object_or_404( )
,而不是直接使用get()
与使用 get() 时一样,我们显式地引发 404 错误。但是,在使用
get_object_or_404
时,它是自动完成的。使用方式如下:
对于批量删除:
对于删除单个实例,请按以下方式使用
get_object_or_404()
而不是get()
:如果未找到,则会自动引发 404。
You can also use
get_object_or_404()
, rather than directly usingget()
as while using get() we explicitly raise the error of 404.But, while using
get_object_or_404
it is automatically done.Use it like:
For bulk deletion:
For deleting single instance, use
get_object_or_404()
instead ofget()
in the following way:If, not found raises 404 automatically.
就像调用以下命令一样简单。
如果您想根据 id 删除多条记录,
使用
__in
查询查找< /a>如果您想删除所有记录,请使用
.all()
检索所有查询,然后
.delete()
。It is as simple as calling the following.
In case you want to remove multiple records based on id,
use the
__in
query lookupIn case you want to delete all records, use
.all()
to retrieve all queries,then
.delete()
.我的做法:
对我来说,这看起来很容易理解,这就是我使用这种方法的原因。
The way I do it:
For me it looks easy to understand, that's why I use this approach.
您可以直接从管理面板删除对象,或者还可以选择通过输入 python3 manage.py shell(Linux 中的 python3)从交互式 shell 中删除特定或选定的 id。
如果您希望用户通过浏览器(提供可视化界面)删除对象,例如数据库中 ID 为 6 的员工,我们可以使用以下代码来实现:
emp = employee.objects.get(id=6).delete()
这将删除 ID 为 6 的员工。
如果您希望删除数据库中存在的所有员工而不是 get(),请指定 all( )如下:
员工.objects.all().delete()
you can delete the objects directly from the admin panel or else there is also an option to delete specific or selected id from an interactive shell by typing in python3 manage.py shell (python3 in Linux).
If you want the user to delete the objects through the browser (with provided visual interface) e.g. of an employee whose ID is 6 from the database, we can achieve this with the following code,
emp = employee.objects.get(id=6).delete()
THIS WILL DELETE THE EMPLOYEE WITH THE ID is 6.
If you wish to delete the all of the employees exist in the DB instead of get(), specify all() as follows:
employee.objects.all().delete()