如何删除 Django 模型中的记录?

发布于 2024-09-25 13:54:06 字数 109 浏览 3 评论 0原文

我想删除特定记录,例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

郁金香雨 2024-10-02 13:54:06

有几种方法:

直接删除:

SomeModel.objects.filter(id=id).delete()

从实例中删除:

instance = SomeModel.objects.get(id=id)
instance.delete()

There are a couple of ways:

To delete it directly:

SomeModel.objects.filter(id=id).delete()

To delete it from an instance:

instance = SomeModel.objects.get(id=id)
instance.delete()
零度° 2024-10-02 13:54:06
MyModel.objects.get(pk=1).delete()

如果具有指定主键的对象不存在,这将引发异常,因为它首先尝试检索指定的对象。

MyModel.objects.filter(pk=1).delete()

如果指定主键的对象不存在并且直接生成查询,则不会引发异常

DELETE FROM my_models where id=1
MyModel.objects.get(pk=1).delete()

this will raise exception if the object with specified primary key doesn't exist because at first it tries to retrieve the specified object.

MyModel.objects.filter(pk=1).delete()

this wont raise exception if the object with specified primary key doesn't exist and it directly produces the query

DELETE FROM my_models where id=1
梦回旧景 2024-10-02 13:54:06

如果您想删除一个实例,请编写代码

entry = Account.objects.get(id=5)
entry.delete()

如果您想删除所有实例,请编写代码

entries = Account.objects.all()
entries.delete()

if you want to delete one instance then write the code

entry = Account.objects.get(id=5)
entry.delete()

if you want to delete all instance then write the code

entries = Account.objects.all()
entries.delete()
羁〃客ぐ 2024-10-02 13:54:06

如果您想删除一件商品

wishlist = Wishlist.objects.get(id = 20)
wishlist.delete()

例如如果您想删除心愿单中的所有商品

Wishlist.objects.all().delete()

If you want to delete one item

wishlist = Wishlist.objects.get(id = 20)
wishlist.delete()

If you want to delete all items in Wishlist for example

Wishlist.objects.all().delete()
十二 2024-10-02 13:54:06

通过 wolph 扩展答案

请注意,您应该将 request 作为参数传递给视图中的删除函数。一个例子是这样的:

from django.shortcuts import redirect

def delete(request, id):
    YourModelName.objects.filter(id=id).delete()

    return redirect('url_name')

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:

from django.shortcuts import redirect

def delete(request, id):
    YourModelName.objects.filter(id=id).delete()

    return redirect('url_name')
别靠近我心 2024-10-02 13:54:06

delete() 方法用于从数据库中删除模型实例。此方法会立即删除对象。该方法返回删除的对象的数量。

示例:

删除一条记录:

data_to_be_deleted = Modelname.objects.get(fieldname = value)
data_to_be_deleted.delete()

由于 get 方法从查询集中返回单个对象,因此只会删除单个记录。如果提供的值不存在,这将引发错误。如果表中存在具有相同值的多条记录,那么它也会被删除抛出错误,因此好的做法是在使用 get 时使用单个唯一值。

根据条件删除多条记录:

对于基于条件的删除,在查询集上使用过滤方法,然后调用删除。

data_to_be_deleted = Modelname.objects.filter(fieldname = value)
data_to_be_deleted.delete()

删除所有记录:

要从数据库表中删除所有模型实例/记录,您需要对所有实例调用删除方法

data_to_be_deleted = Modelname.objects.all()
data_to_be_deleted.delete()

注意:代码可以单行编写为 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:

data_to_be_deleted = Modelname.objects.get(fieldname = value)
data_to_be_deleted.delete()

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.

data_to_be_deleted = Modelname.objects.filter(fieldname = value)
data_to_be_deleted.delete()

For deleting all records:

For deletion of all model instances/records from database table you need to call delete method on all

data_to_be_deleted = Modelname.objects.all()
data_to_be_deleted.delete()

Note: code can be written in single line as Modelname.objects.all().delete(), but for clear understanding, I have used multiple lines.

人生戏 2024-10-02 13:54:06

您还可以使用 get_object_or_404( ),而不是直接使用 get() 与使用 get() 时一样,我们显式地引发 404 错误。

但是,在使用 get_object_or_404 时,它是自动完成的。

get_object_or_404 根据 4.0 文档,在给定模型管理器上调用 get(),但它会引发 Http404 而不是模型的DoesNotExist 异常。

使用方式如下:

对于批量删除:

AnyModel.objects.filter(id=id).delete()

对于删除单个实例,请按以下方式使用 get_object_or_404() 而不是 get()

instance=get_object_or_404(anyModel,id=id)
instance.delete()

如果未找到,则会自动引发 404。

You can also use get_object_or_404(), rather than directly using get() as while using get() we explicitly raise the error of 404.

But, while using get_object_or_404 it is automatically done.

get_object_or_404 According to 4.0 docs, Calls get() on a given model manager, but it raises Http404 instead of the model’s DoesNotExist exception.

Use it like:

For bulk deletion:

AnyModel.objects.filter(id=id).delete()

For deleting single instance, use get_object_or_404() instead of get() in the following way:

instance=get_object_or_404(anyModel,id=id)
instance.delete()

If, not found raises 404 automatically.

青巷忧颜 2024-10-02 13:54:06

就像调用以下命令一样简单。

SomeModel.objects.get(pk=1).delete()
# Or
SomeModel.objects.filter(pk=1).delete()

# SQL equivalent
# delete from table_name where id = 1;

如果您想根据 id 删除多条记录,
使用 __in 查询查找< /a>

SomeModel.objects.fitler(pk__in=[1,2,3,4,5,...]).delete()

# SQL equivalent
# delete from table_name where id in (1,2,4,5,...);

如果您想删除所有记录,请使用 .all() 检索所有查询,
然后.delete()

SomeModel.objects.all().delete()

# SQL equivalent
# delete from table_name;

It is as simple as calling the following.

SomeModel.objects.get(pk=1).delete()
# Or
SomeModel.objects.filter(pk=1).delete()

# SQL equivalent
# delete from table_name where id = 1;

In case you want to remove multiple records based on id,
use the __in query lookup

SomeModel.objects.fitler(pk__in=[1,2,3,4,5,...]).delete()

# SQL equivalent
# delete from table_name where id in (1,2,4,5,...);

In case you want to delete all records, use .all() to retrieve all queries,
then .delete().

SomeModel.objects.all().delete()

# SQL equivalent
# delete from table_name;
客…行舟 2024-10-02 13:54:06

我的做法:

instance = SomeModel.objects.get(id=1)

instance.delete()

对我来说,这看起来很容易理解,这就是我使用这种方法的原因。

The way I do it:

instance = SomeModel.objects.get(id=1)

instance.delete()

For me it looks easy to understand, that's why I use this approach.

绝不放开 2024-10-02 13:54:06

您可以直接从管理面板删除对象,或者还可以选择通过输入 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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文