English 中文(简体)
如何删除Django模式的记录?
原标题:How to delete a record in Django models?

我想删除一个特别的记录:

delete from table_name where id = 1;

我怎么能以暴徒模式这样做?

问题回答

有几个途径:

直接删除:

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

删除:

instance = SomeModel.objects.get(id=id)
instance.delete()
MyModel.objects.get(pk=1).delete()

如果标有特定主要钥匙的物体存在,则这将产生例外情形,因为它首先试图检索特定物体。

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

如果标有特定主要钥匙的物体没有,且直接产生质询,这便会产生例外。

DELETE FROM my_models where id=1

如果你想删除一例,就写法

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

如果你想删除所有内容,那么就写一下该守则

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

请删除一个项目

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

如果你想要删除Wishlist的所有项目,例如

Wishlist.objects.all().delete()

https://stackoverflow.com/users/54017/wolph”

请注意,你应作为参数请求删除你认为的职能。 例如:

from django.shortcuts import redirect


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

return redirect( url_name )

使用<代码>delete()方法从数据库中删除模型。 这种方法立即删除该物体。 这种方法将删除的物体数目回去。

例:

删除一个记录:

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

随着方法的回收,单项物体只能从盘点中删除。 如果供应的价值确实存在,就会犯错误。 如果在表格中有同值的多功能记录,那么也会产生错误,那么良好做法就是在使用时使用独一无二的价值。

根据以下条件删除多个记录:

有条件的删除过滤方法在查询时使用,然后删除。

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(),但为了明确理解,我使用了多个线。

简言之,简言之。

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;

我这样做的方式:

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

instance.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()

附文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()





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签