Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Things to do when remove a model from Django 1.7+

Tags:

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

like image 285
Xiao Liang Avatar asked Mar 14 '15 00:03

Xiao Liang


People also ask

What happens when you delete model database?

Delete a model to remove the model and all of its data from Master Data Services. When you complete this procedure, all objects and all data from all versions of the model will be permanently deleted.

How do I delete an existing model in Django?

Delete a Record of a Model Using the delete() Method in Django. All the model objects or instances in Django have a delete() method, which can be used to delete that record.

What to do after creating models in Django?

Once you have defined your models, you need to tell Django you're going to use those models. Do this by editing your settings file and changing the INSTALLED_APPS setting to add the name of the module that contains your models.py .


1 Answers

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.

like image 183
Daniel Hawkins Avatar answered Sep 28 '22 09:09

Daniel Hawkins