如何使用Django删除表中的所有数据

问题:如何使用Django删除表中的所有数据

我有两个问题:

  1. 如何在Django中删除表格?
  2. 如何删除表格中的所有数据?

这是我的代码,不成功:

Reporter.objects.delete()

I have two questions:

  1. How do I delete a table in Django?
  2. How do I remove all the data in the table?

This is my code, which is not successful:

Reporter.objects.delete()

回答 0

经理内部:

def delete_everything(self):
    Reporter.objects.all().delete()

def drop_table(self):
    cursor = connection.cursor()
    table_name = self.model._meta.db_table
    sql = "DROP TABLE %s;" % (table_name, )
    cursor.execute(sql)

Inside a manager:

def delete_everything(self):
    Reporter.objects.all().delete()

def drop_table(self):
    cursor = connection.cursor()
    table_name = self.model._meta.db_table
    sql = "DROP TABLE %s;" % (table_name, )
    cursor.execute(sql)

回答 1

根据最新文档,正确的调用方法是:

Reporter.objects.all().delete()

As per the latest documentation, the correct method to call would be:

Reporter.objects.all().delete()

回答 2

如果要从所有表中删除所有数据,则可能需要尝试使用命令python manage.py flush。这将删除表中的所有数据,但表本身仍将存在。

在此处查看更多信息:https : //docs.djangoproject.com/en/1.8/ref/django-admin/

If you want to remove all the data from all your tables, you might want to try the command python manage.py flush. This will delete all of the data in your tables, but the tables themselves will still exist.

See more here: https://docs.djangoproject.com/en/1.8/ref/django-admin/


回答 3

使用外壳,

1)删除表:

python manage.py dbshell
>> DROP TABLE {app_name}_{model_name}

2)要从表中删除所有数据:

python manage.py shell
>> from {app_name}.models import {model_name}
>> {model_name}.objects.all().delete()

Using shell,

1) For Deleting the table:

python manage.py dbshell
>> DROP TABLE {app_name}_{model_name}

2) For removing all data from table:

python manage.py shell
>> from {app_name}.models import {model_name}
>> {model_name}.objects.all().delete()

回答 4

Django 1.11从数据库表中删除所有对象-

Entry.objects.all().delete()  ## Entry being Model Name. 

请参考以下引用的Django官方文档-https: //docs.djangoproject.com/en/1.11/topics/db/queries/#deleting-objects

请注意,delete()是唯一未在Manager本身上公开的QuerySet方法。这是一种安全机制,可防止您意外地请求Entry.objects.delete()并删除所有条目。如果确实要删除所有对象,则必须显式请求一个完整的查询集:

我自己尝试了以下代码片段, somefilename.py

    # for deleting model objects
    from django.db import connection
    def del_model_4(self):
        with connection.schema_editor() as schema_editor:
            schema_editor.delete_model(model_4)

在我内部,我views.py有一个视图,只显示一个html页面…

  def data_del_4(request):
      obj = calc_2() ## 
      obj.del_model_4()
      return render(request, 'dc_dash/data_del_4.html') ## 

它结束了从-model == model_4删除所有条目的操作,但是当我尝试确定model_4的所有对象都已删除时,我现在看到管理控制台中的错误屏幕…

ProgrammingError at /admin/dc_dash/model_4/
relation "dc_dash_model_4" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "dc_dash_model_4" 

请考虑一下-如果我们不转到ADMIN控制台并尝试查看模型的对象-已经被删除-Django应用程序将按预期工作。

django管理员截屏

Django 1.11 delete all objects from a database table –

Entry.objects.all().delete()  ## Entry being Model Name. 

Refer the Official Django documentation here as quoted below – https://docs.djangoproject.com/en/1.11/topics/db/queries/#deleting-objects

Note that delete() is the only QuerySet method that is not exposed on a Manager itself. This is a safety mechanism to prevent you from accidentally requesting Entry.objects.delete(), and deleting all the entries. If you do want to delete all the objects, then you have to explicitly request a complete query set:

I myself tried the code snippet seen below within my somefilename.py

    # for deleting model objects
    from django.db import connection
    def del_model_4(self):
        with connection.schema_editor() as schema_editor:
            schema_editor.delete_model(model_4)

and within my views.py i have a view that simply renders a html page …

  def data_del_4(request):
      obj = calc_2() ## 
      obj.del_model_4()
      return render(request, 'dc_dash/data_del_4.html') ## 

it ended deleting all entries from – model == model_4 , but now i get to see a Error screen within Admin console when i try to asceratin that all objects of model_4 have been deleted …

ProgrammingError at /admin/dc_dash/model_4/
relation "dc_dash_model_4" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "dc_dash_model_4" 

Do consider that – if we do not go to the ADMIN Console and try and see objects of the model – which have been already deleted – the Django app works just as intended.

django admin screencapture


回答 5

有两种方法:

要直接删除它:

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

要从实例中删除它:

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

//不要使用相同的名称

There are a couple of ways:

To delete it directly:

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

To delete it from an instance:

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

// don’t use same name