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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4532681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:11:47  来源:igfitidea点击:

How to remove all of the data in a table using Django

pythondjangodjango-queryset

提问by zjm1126

I have two questions:

我有两个问题:

  1. How do I delete a table in Django?
  2. How do I remove all the data in the table?
  1. 如何删除 Django 中的表?
  2. 如何删除表中的所有数据?

This is my code, which is not successful:

这是我的代码,不成功:

Reporter.objects.delete()

采纳答案by Tiago

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)

回答by Ash

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

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

Reporter.objects.all().delete()

回答by user2817997

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.

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

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

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

回答by Ashish Gupta

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

// 不要使用相同的名称

回答by Rohit Dhankar

Django 1.11 delete all objects from a database table -

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

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

参考下面引用的官方 Django 文档 - 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:

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

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

我自己尝试了下面看到的代码片段 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.pyi have a view that simply renders a html page ...

在我的内部我views.py有一个视图,它只是呈现一个 html 页面......

  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 ...

它结束了从 - 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" 

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.

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

django admin screencapture

django 管理屏幕截图

回答by Mangy 007

Using shell,

使用外壳,

1) For Deleting the table:

1)对于删除表:

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

2) For removing all data from table:

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

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