从Django 1.7+中删除模型时需要做的事情 [英] Things to do when remove a model from Django 1.7+

查看:233
本文介绍了从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:


  1. 从model.py
  2. 中删除模型代码
  3. 确保没有其他文件导入此模型或使用它们
    (admin.py,views)。 py等)

  4. 运行 makemigrations 迁移命令

  5. 由于Django不清理数据库,你从你的数据库手动删除
    这个模型的表

  6. 还要注意,有一个名为ContentTypes的表,它保留
    记录关于信息我们您的每个模型。所以你需要手动删除
    这个模型的记录(但我不知道如何做到
    ,有没有人给出一些解释?)

  1. Delete the codes for model from model.py
  2. Make sure that no other file imports this model or uses them (admin.py, views.py, etc)
  3. Run makemigrations and migrate commands
  4. Since Django doesn't clean database for you, you delete the table of this model manually from you database
  5. 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.


  1. 删除所有引用在您的代码中预订模型。例如,删除 BookReview 模型中的 ForeignKey('books.Book')字段。

  2. 书籍模型的代码>书籍/ models.py 。现在,创建迁移( manage.py makemigrations )。如果您查看生成的迁移,它应该包含一个 migrations.DeleteModel 操作。

  3. 运行auto-生成的迁移( manage.py migrate ),您应该被问及有关不再需要的相关 ContentType 对象:

  1. Remove all references to the Book model in your code. For example, remove the ForeignKey('books.Book') field on the BookReview model. There is no need to make a separate migration for this change.
  2. Remove the code for the Book model from books/models.py. Now, create a migration (manage.py makemigrations). If you look at the migration that is generated, it should include a migrations.DeleteModel operation.
  3. Run the auto-generated migration (manage.py migrate), and you should be asked about the relevant ContentType 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆