tags:

views:

95

answers:

2
+1  Q: 

Updating Models

Due to my little confidence with Django and my sheer horror at the thought of seriously messing up my beautiful project, I shall ask for proper advice/instructions here.

The database that my Django project is sitting on top of has been changed (a few field types have been changed) and my models are now out-of-sync. Funnily enough, my Django still works (God knows how) but I still want to update the models. How do I go about doing this the proper way. Thank you very much indeed in advance.


Marked as answered. My actual discover was:

./manage.py inspectdb > <file>
//Hands you all the tables from the database. 
//Then you update the models accordingly.

SIMPLE! :)

+8  A: 

It's probably a bit late, but you might want to take a look at South, which is a migrations system for Django.

The normal practice for your situation would be to run manage.py reset appname, where appname is the name of the app which contains the models you've changed. You'll obviously want to dump the data in the affected tables first (find out what tables are going to be affected by running manage.py sqlreset appname).

Finally, it's quite possible your site is still running happily because you've not restarted the webserver (I'm assuming you're talking about a production environment, the development server reloads most changes automatically).

Dominic Rodger
I haven't used South myself, I worked with django evolution, but I was having some trouble with that and will probably switch to South. I wasn't aware of South, so thanks for sharing!
Nick
@Nick - South works great, mostly. I occasionally get myself into a muddle with it and need to start from a reset again, but I suspect that's more me being incompetent than South.
Dominic Rodger
Be careful with the "dump data -> reset app -> load data" process. There are occasions when the data will not serialize properly and you won't be able to load it. Make sure to BACKUP YOUR DATABASE before you attempt the process
Jiaaro
@Jim - I've not encountered them. How would you work around them if you can't migrate the app? Run the migrations by hand?
Dominic Rodger
+1  A: 

If you've already made the changes to the live database, you can probably just change the models and restart your webserver.

As long as your Field names match between the database and the models you shouldn't have any issues.

That being said, it is a much better idea to use a migration tool like south (as Dominic suggested already)

Jiaaro