从Django 1.7+中删除模型时需要做的事情 [英] Things to do when remove a model from Django 1.7+
问题描述
我想知道如果有任何人可以给出一个完整的列表,当我们想从Django中删除一个模型时,需要完成的事情。我知道有一个类似的问题被问到。但似乎是几年前,当时人们还在使用南方来处理数据库。所以我期待最近版本的Django的答案。
I want to know if any one could give a complete list of things which need to be done when we want to remove a model from Django. I know that a similar question was asked. But it seems to be several years ago, when people were still using South to deal with Database. So I expect an answer for the recent version of Django.
我得出结论,我知道如下:
I conclude what I know as follows:
- 从model.py 中删除模型代码
- 确保没有其他文件导入此模型或使用它们
(admin.py,views)。 py等) - 运行 makemigrations 和迁移命令
- 由于Django不清理数据库,你从你的数据库手动删除
这个模型的表 - 还要注意,有一个名为ContentTypes的表,它保留
记录关于信息我们您的每个模型。所以你需要手动删除
这个模型的记录(但我不知道如何做到
,有没有人给出一些解释?)
- Delete the codes for model from model.py
- Make sure that no other file imports this model or uses them (admin.py, views.py, etc)
- Run makemigrations and migrate commands
- Since Django doesn't clean database for you, you delete the table of this model manually from you database
- Also note that there is a table called ContentTypes, which keeps records about the info our your every model. So you need to delete the record for this model manually (But I don't know how to do it exactly. Would any one give some explanation?)
这些都是我所知道的。有什么不对吗我忘了什么吗?也许我过于谨慎,但我想保持数据库的清洁。
非常感谢!
These are all the things I know. Is there anything wrong? And did I forget anything? Maybe I'm over-cautious, but I'd like to keep the database clean. Thanks a lot!
推荐答案
在Django 1.7中,这实际上比你想象的要简单得多。假设您有一个应用程序书籍
,其中有两种型号:书
和 BookReview
。您要删除
书籍
模型。
In Django 1.7, this is actually much simpler than you think. Let's say you have an app, books
, with two models: Book
and BookReview
. You want to remove the Book
model.
- 删除所有引用
在您的代码中预订
模型。例如,删除BookReview
模型中的ForeignKey('books.Book')
字段。 - 从
书籍
模型的代码>书籍/ models.py 。现在,创建迁移(manage.py makemigrations
)。如果您查看生成的迁移,它应该包含一个migrations.DeleteModel
操作。 -
运行auto-生成的迁移(
manage.py migrate
),您应该被问及有关不再需要的相关ContentType
对象:
- Remove all references to the
Book
model in your code. For example, remove theForeignKey('books.Book')
field on theBookReview
model. There is no need to make a separate migration for this change. - Remove the code for the
Book
model frombooks/models.py
. Now, create a migration (manage.py makemigrations
). If you look at the migration that is generated, it should include amigrations.DeleteModel
operation. Run the auto-generated migration (
manage.py migrate
), and you should be asked about the relevantContentType
objects that are no longer needed:
Running migrations:
Applying books.0002_auto_20150314_0604... OK
The following content types are stale and need to be deleted:
books | book
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
您可能想要删除内容类型。如果您不想要输入内容,可以使用 manage.py migrate --noinput
。
You probably do want to delete the content types. If you don't want to be asked for input, you can use manage.py migrate --noinput
.
此次迁移中的 DeleteModel
操作将删除数据库中的 books_book
表,因此您不用担心关于手动清理。
The DeleteModel
operation in this migration will drop the books_book
table in your database, so you don't have to worry about manually cleaning up at all.
这篇关于从Django 1.7+中删除模型时需要做的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!