MacPython
MacPython

Reputation: 18271

Django db Table delete

With what command can I delete tables from a django db of a specific app/ only one table? I did find all sorts of things, but not how to get rid of a table.

Also while I am at this.

I create the models and hit syncdb and then I have the tables. If I want to update/add to those tables, do I run into problems?

Upvotes: 1

Views: 5086

Answers (3)

Izz ad-Din Ruhulessin
Izz ad-Din Ruhulessin

Reputation: 6175

def reset():
    import install
    from django.db.models import get_models
    removed_tables = []
    exceptions = [] 
    for model in get_models():
        if model.__name__ not in ('User','Session','Group','Permission'):
            try:
                model.objects.all().delete() # So we can remove the table without complaints from the database server.
                CURSOR.execute('DROP TABLE %s' % model._meta.db_table)
                print "Dropped table %s from model %s" % (model._meta.db_table, model.__name__)
            except Exception as e:
                exceptions.append([model._meta.db_table, str(e)])
                continue
            removed_tables.append(model._meta.db_table)
    print "Removed %s tables" % len(removed_tables)
    syncdb()
    install.install() # A function that leads to the creation of my default data

Upvotes: 0

ApPeL
ApPeL

Reputation: 4911

Your best bet would be to get django-south installed in your machine.

if you are using pip, do pip install django-south

This allows you too migrate data forward and backwards.

This is very handy especially if you need to update tables, and new tables etc.

check it out.

adding south to apps are as easy as python manage.py schemamigration appname --initial

Make your changes in a model and run the following python manage.py schemamigration appname --auto

Once your data migration file has been created it'll tell you data is now ready to migrate.

Simply use python manage.py migrate appname

http://south.aeracode.org/docs/about.html

Hope this helps

Upvotes: 4

Rick
Rick

Reputation: 17013

If you are deleting a table, this is done in the model file of the specific app that you are trying to delete, there is no command for this, you just go into the file and delete it and then re-run syncdb, for your other question, the answer is the same.. every app folder should have a file called "models.py" and here is where the models which are, in this case, equivalent to tables are specified, along with their fields, you simply edit this to make any changes.

Upvotes: 0

Related Questions