Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django数据库表删除_Django_Database - Fatal编程技术网

Django数据库表删除

Django数据库表删除,django,database,Django,Database,使用什么命令可以从特定应用程序的django db中删除表/仅删除一个表? 我确实找到了各种各样的东西,但没有找到如何摆脱桌子的方法 当我在这里的时候 我创建模型并点击syncdb,然后我就有了表格。 如果我想更新/添加这些表,我会遇到问题吗?如果您要删除表,这是在您试图删除的特定应用程序的模型文件中完成的,没有相应的命令,您只需进入文件并删除它,然后重新运行syncdb,对于其他问题,答案是相同的。。每个应用程序文件夹都应该有一个名为“models.py”的文件,在这里指定了与表等效的模型及其

使用什么命令可以从特定应用程序的django db中删除表/仅删除一个表? 我确实找到了各种各样的东西,但没有找到如何摆脱桌子的方法

当我在这里的时候

我创建模型并点击syncdb,然后我就有了表格。
如果我想更新/添加这些表,我会遇到问题吗?

如果您要删除表,这是在您试图删除的特定应用程序的模型文件中完成的,没有相应的命令,您只需进入文件并删除它,然后重新运行syncdb,对于其他问题,答案是相同的。。每个应用程序文件夹都应该有一个名为“models.py”的文件,在这里指定了与表等效的模型及其字段,您只需编辑此文件即可进行任何更改。

如果要删除表,则在您试图删除的特定应用程序的模型文件中执行此操作,没有这个命令,您只需进入文件并删除它,然后重新运行syncdb,对于您的另一个问题,答案是相同的。。每个应用程序文件夹都应该有一个名为“models.py”的文件,在这里指定了与表等效的模型及其字段,您只需编辑此文件即可进行任何更改。

您最好的选择是在您的计算机中安装django south

如果您正在使用pip,请执行
pip安装django south

这也允许您向前和向后迁移数据

这非常方便,尤其是当您需要更新表和新表等时

看看吧

将south添加到应用程序与python manage.py schemamigration appname--initial

在模型中进行更改并运行以下
python manage.py schemamigration appname--auto

创建数据迁移文件后,它会告诉您数据现在已准备好迁移

只需使用
python manage.py迁移appname


希望这对您有所帮助

您最好的选择是在您的机器中安装django south

def reset():
    import install
    from django.db.models import get_models
    removed_tables = []
    exceptions = [] 
    for model in get_models():
        if model.__name__ not in ('User','Session','Group','Permission'):
            try:
                model.objects.all().delete() # So we can remove the table without complaints from the database server.
                CURSOR.execute('DROP TABLE %s' % model._meta.db_table)
                print "Dropped table %s from model %s" % (model._meta.db_table, model.__name__)
            except Exception as e:
                exceptions.append([model._meta.db_table, str(e)])
                continue
            removed_tables.append(model._meta.db_table)
    print "Removed %s tables" % len(removed_tables)
    syncdb()
    install.install() # A function that leads to the creation of my default data
如果您正在使用pip,请执行
pip安装django south

这也允许您向前和向后迁移数据

这非常方便,尤其是当您需要更新表和新表等时

看看吧

将south添加到应用程序与python manage.py schemamigration appname--initial

在模型中进行更改并运行以下
python manage.py schemamigration appname--auto

创建数据迁移文件后,它会告诉您数据现在已准备好迁移

只需使用
python manage.py迁移appname


希望这能有所帮助

ApPeL的回答为我提供了一个新的视角,说明了为什么您可能会这样做,我猜您是在尝试将现有数据库与django安装同步?无论如何,一旦你在Django安装中工作,你应该总是通过Django创建新的模型/表,因为它会为你做这件事(自动创建DB表),但是我想当你删除模型时它不会删除表?如果是这样的话,ApPel的解决方案将是正确的,因为它只需要一个进入数据库的脚本来同步它。我可以准确地告诉您我为什么要这样做。我只在django工作。我正试图让一个相当复杂的数据库运行。所以现在我正在尝试它的不同变体。为了避免数据库混乱,我想删除它。也。我想知道,万一以后我启动并运行时,我想删除特定的表,我想知道这是否可行以及如何实现。ApPeL的回答让我从一个新的角度了解了为什么您可能会这样做,我猜您是在尝试将现有数据库与django安装同步?无论如何,一旦你在Django安装中工作,你应该总是通过Django创建新的模型/表,因为它会为你做这件事(自动创建DB表),但是我想当你删除模型时它不会删除表?如果是这样的话,ApPel的解决方案将是正确的,因为它只需要一个进入数据库的脚本来同步它。我可以准确地告诉您我为什么要这样做。我只在django工作。我正试图让一个相当复杂的数据库运行。所以现在我正在尝试它的不同变体。为了避免数据库混乱,我想删除它。也。我想知道,万一以后我启动并运行时想删除特定的表,我想知道这是否可行,以及如何实现。酷。谢谢听说了。pip是内置的还是我必须得到它?pip不是标准的,但易于安装。在terminal,easy_install Pip中运行以下命令。如果要求不多,您也可以向上投票。酷。谢谢听说了。pip是内置的还是我必须得到它?pip不是标准的,但易于安装。在terminal,easy_install Pip中运行以下命令。如果要求不太多,您也可以向上投票。
def reset():
    import install
    from django.db.models import get_models
    removed_tables = []
    exceptions = [] 
    for model in get_models():
        if model.__name__ not in ('User','Session','Group','Permission'):
            try:
                model.objects.all().delete() # So we can remove the table without complaints from the database server.
                CURSOR.execute('DROP TABLE %s' % model._meta.db_table)
                print "Dropped table %s from model %s" % (model._meta.db_table, model.__name__)
            except Exception as e:
                exceptions.append([model._meta.db_table, str(e)])
                continue
            removed_tables.append(model._meta.db_table)
    print "Removed %s tables" % len(removed_tables)
    syncdb()
    install.install() # A function that leads to the creation of my default data