Delete a field and delete a model from a database with Django

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.