Delete a field
It is much easier to remove a field from the model than to add it. Delete The fields, just a few steps:
- Delete the fields, and then restart your Web server.
- Remove the fields from the database with the following command:
ALTER TABLE books_book DROP COLUMN num_pages;
Make sure that the operation is in the correct order. If you delete a field from the database first, Django throws an exception immediately.
Delete Many-to-many association fields
Because many-to-many association fields are different from normal fields, the delete operation is different.
- Remove Manytomanyfield from your model, and then restart the Web server.
- Remove the associated table from the database with the following command:
DROP TABLE books_book_authors;
As above, note the order of operations.
Deleting a model
It is easier to delete an entire model than to delete a field. Deleting a model takes the following steps:
- Remove the model you want to delete from the file, and then restart the Web server models.py
- Then delete the table from the database with the following command:
DROP TABLE Books_book;
- Be careful when you need to remove any dependent tables from the database (that is, any table that has a foreign key to the table Books_book).
As in the previous section, be sure to do so in this order.