Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update django database to reflect changes in existing models

I've already defined a model and created its associated database via manager.py syncdb. Now that I've added some fields to the model, I tried syncdb again, but no output appears. Upon trying to access these new fields from my templates, I get a "No Such Column" exception, leading me to believe that syncdb didn't actually update the database. What's the right command here?

like image 500
theactiveactor Avatar asked Dec 31 '09 13:12

theactiveactor


People also ask

How do you add a new field to a model with new Django migrations?

To answer your question, with the new migration introduced in Django 1.7, in order to add a new field to a model you can simply add that field to your model and initialize migrations with ./manage.py makemigrations and then run ./manage.py migrate and the new field will be added to your DB.

How do I update model fields in Django?

Use update_fields in save() If you would like to explicitly mention only those columns that you want to be updated, you can do so using the update_fields parameter while calling the save() method. You can also choose to update multiple columns by passing more field names in the update_fields list.

How do I update Django bulk records?

The best solutions I found are: a) use @transaction. atomic decorator, which improves performance by using a single transaction, or b) make a bulk insert in a temporary table, and then an UPDATE from the temporary table to the original one.

How do I save changes in Django model?

save() method can be used to insert new record and update existing record and generally used for saving instance of single record(row in mysql) in database. update() is not used to insert records and can be used to update multiple records(rows in mysql) in database.


2 Answers

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.

like image 171
zlovelady Avatar answered Sep 22 '22 18:09

zlovelady


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 
like image 37
Cassidy Laidlaw Avatar answered Sep 22 '22 18:09

Cassidy Laidlaw