Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove models from django?

Tags:

django

In Django how do you remove models that you have synced into the database?

For example in Django tutorial page had this following code

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 choice to create the tables into the database. But what if I did something wrong and don't want that model any more. What's the syntax to remove it?

like image 642
starcorn Avatar asked Dec 21 '11 22:12

starcorn


People also ask

How do I delete models 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.

How do I delete a Django record?

To delete a record we do not need a new template, but we need to make some changes to the members template. Of course, you can chose how you want to add a delete button, but in this example, we will add a "delete" link for each record in a new table column. The "delete" link will also contain the ID of each record.

How delete all data Django?

If you want to remove all the data from all your tables, you might want to try the command python manage.py flush . This will delete all of the data in your tables, but the tables themselves will still exist. He did say "all the data from all your tables", which should indicate that it's a very destructive operation.


2 Answers

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};
like image 140
juliomalegria Avatar answered Oct 16 '22 02:10

juliomalegria


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.

like image 33
Cesar Canassa Avatar answered Oct 16 '22 02:10

Cesar Canassa