[Django]-How to remove models from django?

18👍

There is no syntax. Django doesn’t removes tables or columns. You have to manually change your database or use a migration tool like South.

If you justing playing around with tutorial the easier way is to delete your sqlite database file and run a sync again.

30👍

If you don’t want to delete and re-sync your current database, the best way is to drop the table from your model manually:

$ python manage.py dbshell

> (Inside the DB shell)
> DROP TABLE {app-name}_{model-name};

21👍

Why not simply try deleting the models from your models.py file? When you run

python manage.py makemigrations

the migrations file should be updated with the deleted models.

👤pupher

4👍

The most easiest solution is to just delete your model from models.py and run

python3 manage.py makemigrations

(Note: Remove the model from everywhere where you have imported it like admin.py, views.py, or any other file where you have imported it)

3👍

Commenting out the class that defines the model did it for me. Once I had done it and ran python manage.py makemigrations,
I got this as response:
- Delete model MyModel.
Checked afterwards with a DB Browser and it was actually removed.

1👍

If you are facing issue to update changes onto DB so you can directly run this command.

python manage.py migrate --run-syncdb

1👍

I found a simpler method, by sheer experimentation. Even after deleting tables, Django was not making the migrations, so I did the following:

Simply delete the files created in your myapp->migrations directory, making sure that you do not delete the init.py and pycache

  1. Starting from 001initial.py and downwards delete the files.

  2. Run python manage.py makemigrations

  3. Run python manage.py migrate

-M

0👍

Django’s database handling through syncdb is purely additive: any new models will be added, but deleted models will not be deleted and modified models will not be modified.

If you do not have any data you want to preserve, you are fine just dropping and recreating the database: if you have anything you want to preserve, or even if you intend to have anything you want to preserve, I cannot advise you strongly enough to use a migration tool: South has been the de facto standard for every project I’ve worked on.

0👍

Since the Migration command handle Model(database) you can do following steps.

First type

python manage.py makemigrations app_name # it will restructure your model

then type

python manage.py migrate app_name # it will apply to restructure your database.

Example:
I had Posts and PostDetail model,
later on, I wanted to remove PostDetail model and some fields(columns) from Posts model too.

I simply run migrations and migrate commands,checked in Mysql Database. It worked fine.

Hope it will work for you too.

0👍

Weather you’re removing a single model or a full app, you should first
remove the desired models from the models.py file.

Moreover, you have to make sure that no other file imports these
models or uses them (admin.py, views.py, etc).

Next, run South or migrate your database to properly delete these
models from your database.

Check the source of this information on the link below:

http://www.marinamele.com/how-to-correctly-remove-a-model-or-app-in-django

0👍

  1. Comment out the class you want to delete in models.py.
  2. Comment out any other references to the model e.g. in admin.py.
  3. Run the following commands in terminal:
    • python manage.py makemigrations <your_app>

    • python manage.py migrate

This should remove the related table from the database.

Leave a comment