211👍
If you don’t care about data:
Best way would be to drop the database and run syncdb
again. Or you can run:
For Django >= 1.5
python manage.py flush
For Django < 1.5
python manage.py reset appname
(you can add --no-input
to the end of the command for it to skip the interactive prompt.)
If you do care about data:
From the docs:
syncdb will only create tables for
models which have not yet been
installed. It will never issue ALTER
TABLE statements to match changes made
to a model class after installation.
Changes to model classes and database
schemas often involve some form of
ambiguity and, in those cases, Django
would have to guess at the correct
changes to make. There is a risk that
critical data would be lost in the
process.If you have made changes to a model
and wish to alter the database tables
to match, use the sql command to
display the new SQL structure and
compare that to your existing table
schema to work out the changes.
https://docs.djangoproject.com/en/dev/ref/django-admin/
Reference: FAQ – https://docs.djangoproject.com/en/dev/faq/models/#if-i-make-changes-to-a-model-how-do-i-update-the-database
People also recommend South ( http://south.aeracode.org/docs/about.html#key-features ), but I haven’t tried it.
6👍
Using Django Extensions, running:
./manage.py reset_db
Will clear the database tables, then running:
./manage.py syncdb
Will recreate them (south may ask you to migrate things).
- [Django]-How to add custom field in ModelSerializer?
- [Django]-Can we append to a {% block %} rather than overwrite?
- [Django]-Why am I getting this error in Django?
4👍
I think Django docs explicitly mention that if the intent is to start from an empty DB again (which seems to be OP’s intent), then just drop and re-create the database and re-run migrate
(instead of using flush
):
If you would rather start from an empty database and re-run all
migrations, you should drop and recreate the database and then run
migrate instead.
So for OP’s case, we just need to:
- Drop the database from MySQL
- Recreate the database
- Run
python manage.py migrate
- [Django]-How to delete a record in Django models?
- [Django]-Unittest Django: Mock external API, what is proper way?
- [Django]-Django TemplateSyntaxError – 'staticfiles' is not a registered tag library
0👍
Quickest (drops and creates all tables including data):
./manage.py reset appname | ./manage.py dbshell
Caution:
- Might not work on Windows correctly.
- Might keep some old tables in the db
- [Django]-Inject errors into already validated form?
- [Django]-Django Cache cache.set Not storing data
- [Django]-What are the differences between django-tastypie and djangorestframework?
0👍
You can use the Django-Truncate library to delete all data of a table without destroying the table structure.
Example:
- First, install django-turncate using your terminal/command line:
pip install django-truncate
- Add "django_truncate" to your INSTALLED_APPS in the
settings.py
file:
INSTALLED_APPS = [
...
'django_truncate',
]
- Use this command in your terminal to delete all data of the table from the app.
python manage.py truncate --apps app_name --models table_name
- [Django]-Django development IDE
- [Django]-Get last record in a queryset
- [Django]-Django – filtering on foreign key properties