Things to do when remove a model from Django 1.7+

前端 未结 1 1925
日久生厌
日久生厌 2021-02-07 02:45

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

相关标签:
1条回答
  • 2021-02-07 03:21

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

      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.

      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.

    0 讨论(0)
提交回复
热议问题