How to remove models from django?

In Django, how do you delete the models that you synchronized in the database?

For example, in the Django training page, the following code was given

from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models.ForeignKey(Poll) choice = models.CharField(max_length=200) votes = models.IntegerField() 

Then I used python manage.py sql polls and python manage.py sql polls to create tables in the database. But what if I did something wrong and no longer want this model. What syntax to remove?

+7
source share
5 answers

There is no syntax. Django does not delete tables or columns. You must manually modify your database or use a migration tool such as South.

If you want to play with the tutorial, the easiest way is to delete the sqlite database file and start synchronization again.

+12
source

If you do not want to delete and re-synchronize the current database, the best way is to manually delete the table from your model:

 $ python manage.py dbshell > (Inside the DB shell) > DROP TABLE {app-name}_{model-name}; 
+18
source

Why not just try to remove the models from the models.py file? When you run

python manage.py makemigrations

The migration file must be updated using remote models.

+1
source

Processing Djangos database via syncdb is purely additive: any new models will be added, but deleted models will not be deleted, and modified models will not be changed.

If you don’t have the data you want to save, you simply drop and recreate the database: if you have something that you want to save, or even if you intend to have something that you want to save, I can’t You are strong enough to use the migration tool : South was the de facto standard for every project Ive worked on.

0
source

I tried to comment on the model I wanted to remove and added a new one. Then, after I did python manage.py makemigrations, he asked me if I renamed my model. I answered no, and the previous (commented) model was deleted. So I think that removes this model from the database

0
source

All Articles