33👍
Seems like what you need is a migration system. South is really nice, working great, has some automation tools to ease your workflow. And has a great tutorial.
note: syncdb can’t update your existing tables. Sometimes it’s impossible to decide what to do automagicly – that’s why south scripts are this great.
138👍
As of Django 1.7+, built-in migrations support, allows for database schema migrations that preserve data. That’s probably a better approach than the solution below.
Another option, not requiring additional apps, is to use the built in manage.py
functions to export your data, clear the database and restore the exported data.
The methods below will update the database tables for your app, but will completely destroy any data that existed in those tables. If the changes you made to your app model do not break your old schema (for instance, you added a new, optional field) you can simply dump the data before and reload it afterwards, like so:
Django 1.4.15 and earlier
python manage.py dumpdata <your_app> > temp_data.json
python manage.py reset <your_app>
python manage.py loaddata temp_data.json
Django 1.5 and newer
python manage.py dumpdata <your_app> > temp_data.json
python manage.py sqlclear <your_app> | python manage.py dbshell
python manage.py syncdb
python manage.py loaddata temp_data.json
(The reset
command was deprecated and then removed in Django 1.5)
If your changes break your old schema this won’t work – in which case tools like South or Django Evolution are great.
- [Django]-Django storages aws s3 delete file from model record
- [Django]-Iterate over model instance field names and values in template
- [Django]-Annotate a queryset with the average date difference? (django)
44👍
As of Django 1.7, you can now do this with native migrations. Just run
python manage.py makemigrations <your app name>
python manage.py migrate
- [Django]-How do you use the django-filter package with a list of parameters?
- [Django]-Make the first letter uppercase inside a django template
- [Django]-How do I use Django templates without the rest of Django?
9👍
Django’s syncdb doesn’t alter existing tables in the database so you have to do it manually. The way I always do it is:
- Change the model class first.
- Then run: manage.py sql myapp.
- Look at the sql it prints out and see how it represented the change you are going to make.
- Make the change manually using your database manager.
- Check to see if everything worked correctly using the admin site.
If you are using sqllite a good manager is the firefox plugin: link
- [Django]-Where to put business logic in django
- [Django]-AttributeError: 'module' object has no attribute 'tests'
- [Django]-How can I save my secret keys and password securely in my version control system?
8👍
Another tool would be django evolution. No table dropping needed in most cases.
Just install it as any other django app and run:
python manage.py evolve –hint –execute
- [Django]-Does SQLAlchemy have an equivalent of Django's get_or_create?
- [Django]-Deploying Django with gunicorn and nginx
- [Django]-Can a dictionary be passed to django models on create?
3👍
deseb is a great tool for that.
Having it installed, you can write ./manage.py sqlevolve and it’ll generate sql commands necessary to keep the database structure in sync with your models.
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)
- [Django]-How to understand lazy function in Django utils functional module
- [Django]-How to stop gunicorn properly
2👍
You need to drop your tables before you can recreate them with syncdb
.
If you want to preserve your existing data, then you need to unload your database,
drop your tables, run syncdb
to build a new database, then reload your old data into your new tables.
There are tools that help with this. However, in many cases, it’s just as easy to do it manually.
- [Django]-What are the differences between django-tastypie and djangorestframework?
- [Django]-How exactly do Django content types work?
- [Django]-How to write django test meant to fail?
1👍
For versions 1.4.1 and above users the command has changed to
python manage.py flush
Please read the official document before using it as it will delete all your data.
- [Django]-Determine complete Django url configuration
- [Django]-How do I create a slug in Django?
- [Django]-Django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead