Django for ISchoolers: Difference between revisions

no edit summary
imported>Aldeka
No edit summary
imported>Aldeka
No edit summary
Line 481:
 
== Database migrations and South, part one ==
 
When you create your models, you might not always know exactly what fields your models will need in advance. Maybe someday your qandabear app will have users, and you’ll want to keep track of the author of each question! Then you would want to add another field to the model to store that information. Maybe someday you'll decide that tracking all the pub_dates is silly, and want to delete that field.
 
Unfortunately, Django (and most database-using software) can’t figure out how to handle model changes very well on its own. Fortunately, a Django app called `south` can handle these changes–called ‘migrations’–for us.
 
Now that we’ve made our first version of our models file, let’s set up our qandabear app to work with South so that we can make migrations with it in the future!
 
* On the command line, write:
 
<code>$ python manage.py schemamigration qandabear --initial</code>
 
As you can see, that’s created a migrations directory for us, and made a new migration inside it.
 
* All we need to do now is apply our new migration:
 
<code>$ python manage.py migrate qandabear</code>
 
Great! Now our database file knows about qandabear and its new models, and if we need to change our models, South is set up to handle those changes. We’ll come back to South later.
 
IMPORTANT: You can't migrate an app if it's already been synced in the database using <code>python manage.py syncdb</code>. But you do need to run syncdb at least once before you use south (since south itself uses syncdb to give itself space in your database). That's why it's super important that when you run syncdb, south should be listed under INSTALLED_APPS, but none of your own apps should be, and after you add your app to INSTALLED_APPS, you must not run syncdb again until after you've already set up migrations with that app.
 
* Add and commit all your work, including the migrations folder that South generated for you.
 
== Let's add some data (via the Django shell)! ==
Anonymous user