开发者

Problem updating fields in Django app

I am trying to create a new website with Django. I am only just starting out with web development however and am new to many bits and pieces e.g. SQL.

I defined a model with various fields and then went ahead a开发者_Python百科nd created an instance of it etc in the Django shell. Later I changed the name of a few of the fields, now when I try to do things with my model like Post.objects.all() I get errors such as:

DatabaseError: no such column: posts_post.body

How can I update my database entries (or just delete them) so I can get get on with my site?

Thanks


Solution for that has a name, South, I use south for all my django sites! it is really useful and easy to use after you got used to it! give it a shoot!

using south to migrate that table you will need to run 2 commands:

First, to create the migrate script:

python manage.py schemamigration posts --auto

Afterwars to actual apply the table changes:

python manage.py migrate posts


You added new attribute with name "body" to your model "Post" of your application "posts". Django cant't automatically create database column for this new field (it can only create database table for new model). So you need to create all new fields manually. Alter your table in database:

ALTER TABLE `posts_post` ADD COLUMN `body` Text; 


You almost certainly haven't run manage.py syncdb since adding that field to your model.

If you ran syncdb and then added the field to your model, you will need to delete the table and start again with syncdb.

Alternatively, install South since it does this sort of schema upgrading for you.


manage.py syncdb doesnt deals with existing models though it creates table for new models . For this u can use South after installing south run python manage.py schemamigration myapp --auto (for existing models) or python manage.py schemamigration myapp --initial (for new models) and then python manage.py migrate myapp

After using South you will not able to run manage.py syncdb for creating models and south makes a directory where it stores the migration files

When you next time setup the app, you have to run python manage.py migrate to make the tables for models in the database

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜