Adding fields to an already existing database for Django (version < 1.7)

后端 未结 3 567
执笔经年
执笔经年 2021-01-04 11:35

I\'m using Django ( < v1.7), with SQLite3 as the database engine.

I am trying to add a new field to an already existing model\'s class. This class already has dat

相关标签:
3条回答
  • 2021-01-04 12:15

    There are other options of migration apps (although South is the most used).

    I have used django-evolution for my projects and it was very easy to install and start using.

    South seems to be more complete, but for simpler tasks, django-evolution may be suitable.

    0 讨论(0)
  • 2021-01-04 12:16

    Install south in your django and you can easily handle the existing tables. check this

    If you really want to use Django-South, install it on your django, after adding your new fields to your existing model run

    python manage.py schemamigration --initial

    It will create a file in your project app. then,

    python manage.py migrate

    thats it your table is altered.

    0 讨论(0)
  • 2021-01-04 12:20

    This answer is still getting visibility but is outdated. Since 1.7 Django ships with a built-in migration system, written by the same author as South, and has deprecated syncdb though it will still work.

    You will simply need to run a few commands to automatically add new columns:

    python manage.py makemigrations  
    python manage.py migrate
    

    It will be useful to understand what's happening under the hood, but those are the basics. Please ask new questions to get answers on 1.7 and migrations if you are still reading this old post.


    For django < 1.7

    syncdb will not add any new columns. See http://docs.djangoproject.com/en/dev/ref/django-admin/#syncdb

    You will have to add them manually. For example,. replace <> with relevant info:

    python manage.py dbshell
    ALTER TABLE <appname_modelname> ADD COLUMN <column_type> DEFAULT '';
    

    You can see what Django might have done to generate columns on a fresh syncdb by using:

    python manage.py sqlall app_name
    

    and copying and pasting ALTER TABLE statements from there.

    Otherwise, you can look into third-party apps like Django-South which are database migration tools.

    0 讨论(0)
提交回复
热议问题