40👍
Another simple way to do this while using Django 1.4 or below, would be
python manage.py reset app_name
which drops and re-creates the tables used by the models of this app.
This was deprecated in Django 1.3 and is no longer available from Django 1.5
72👍
to clear out an application is as simple as writing:
./manage.py sqlclear app_name | ./manage.py dbshell
then in order to rebuild your tables just type:
./manage.py syncdb
- [Django]-Django: TemplateSyntaxError: Could not parse the remainder
- [Django]-How to run celery as a daemon in production?
- [Django]-Retrieving parameters from a URL
47👍
None of the answers shows how to delete just one table in an app. It’s not too difficult. The dbshell
command logs the user into the sqlite3 shell.
python manage.py dbshell
When you are in the shell, type the following command to see the structure of your database. This will show you all the table names in the database (and also the column names within tables).
SELECT * FROM sqlite_master WHERE type='table';
In general, Django names tables according to the following convention: “appname_modelname”. Therefore, SQL query that accomplishes your goal will look similar to the following:
DROP TABLE appname_modelname;
This should be sufficient, even if the table had relationships with other tables. Now you can log out of SQLITE shell by executing:
.exit
If you run syncdb again, Django will rebuild the table according to your model. This way, you can update your database tables without losing all of the app data. If you are running into this problem a lot, consider using South – a django app that will migrate your tables for you.
- [Django]-Catching DoesNotExist exception in a custom manager in Django
- [Django]-Multiple django sites with apache & mod_wsgi
- [Django]-Django 1.10.1 'my_templatetag' is not a registered tag library. Must be one of:
25👍
get the DROP statements with
python manage.py sqlclear app_name
then try
python manage.py dbshell
and execute the DROP statement
check out http://docs.djangoproject.com/en/dev/ref/django-admin/
- [Django]-How to test "render to template" functions in django? (TDD)
- [Django]-How can I temporarily disable a foreign key constraint in MySQL?
- [Django]-Django form: what is the best way to modify posted data before validating?
19👍
I had the same problem.
For a quick resolve (if you don’t care about losing your tables/data), correct your models.py file with the desired data types, delete the Migration folder and db.SQLite3 file,
then re-run the following commands:
- python manage.py migrate
- python manage.py makemigrations
- python manage.py migrate
- python manage.py createsuperuser (to create an admin user/pswd to manage admin page)
- python manage.py runserver
- [Django]-How does django handle multiple memcached servers?
- [Django]-How to get the value of a Django Model Field object
- [Django]-How to move a model between two Django apps (Django 1.7)
5👍
In Django 2.1.7, I’ve opened the db.sqlite3
file in SQLite browser (there is also a Python package on Pypi) and deleted the table using command
DROP TABLE appname_tablename;
and then
DELETE FROM django_migrations WHERE App='appname';
Then run again
python manage.py makemigrations appname
python manage.py migrate appname
- [Django]-What is the easiest way to clear a database from the CLI with manage.py in Django?
- [Django]-Is it possible to pass query parameters via Django's {% url %} template tag?
- [Django]-Django TemplateSyntaxError – 'staticfiles' is not a registered tag library
2👍
Might be worth expanding on a few answers here:
So, you can get access to the dbshell
with the following command:
python manage.py dbshell
It is then preferential to use the following:
DROP TABLE appname_tablename CASCADE;
To drop any related tables, effectively mirroring the “on_delete=CASCADE” direction on a field.
- [Django]-How should I write tests for Forms in Django?
- [Django]-The number of GET/POST parameters exceeded settings.DATA_UPLOAD_MAX_NUMBER_FIELDS
- [Django]-Python Django Gmail SMTP setup
1👍
In Django 1.9 I had to do Kat Russo’s steps, but the second migration was a little bit tricky. You have to run
./manage.py migrate --run-syncdb
- [Django]-Django's Double Underscore
- [Django]-Django modifying the request object
- [Django]-Django Celery Logging Best Practice