Django: Change models without clearing all data?

前端 未结 3 1930
孤城傲影
孤城傲影 2021-02-05 11:37

I have some models I\'m working with in a new Django installation. Is it possible to change the fields without losing app data?

I tried changing the field and running

3条回答
  •  太阳男子
    2021-02-05 12:17

    You'll have to manually update the database schema/layout, if you're only talking about adding/removing columns.

    If you're attempting to rename a column, you'll have to find another way.

    You can use the python manage.py sql [app name] (http://docs.djangoproject.com/en/dev/ref/django-admin/#sql-appname-appname) command to see what the new SQL should look like, to see what columns, of what type/specification Django would have you add, and then manually run corresponding ALTER TABLE commands.

    There are some apps/projects that enable easier model/DB management, but Django doesn't support this out of the box, on purpose/by design.

提交回复
热议问题