Renaming an app with Django and South

前端 未结 6 777
栀梦
栀梦 2020-12-13 02:28

I am renaming an application to a more suitable name. In doing so, I want to ensure that South properly migrates the database (renames database tables and changes reference

6条回答
  •  天命终不由人
    2020-12-13 02:49

    It is possible to rename an app. As example project, see:

    https://github.com/ASKBOT/django-south-app-rename-example

    Basically, there are 2 migrations. First the tables are renamed using a db.rename_table(), and next the content types are updated. This can be combined into one migration by checking for if not db.dry_run:. See How do I migrate a model out of one django app and into a new one? for an example of that.

    For a initial migrations, you can directly rename the existing tables, if they are there:

    if 'old_app_table_name' in connection.introspection.table_names():
        db.rename_table('old_app_table_name', 'new_app_table_name')
    else:
        # Create new app tables directly.
    

    For tables with more migrations, you may need to check whether the old migration name was already applied:

    from south.models import MigrationHistory
    if MigrationHistory.objects.exists(app_name='old_app', migration='0001_initial'):
        return
    

    Lastly, I recommend using an IDE (e.g. a PyCharm trial) to rename the package (right click, refactor -> rename on the package) because it will update all usages across the application for you, including the URLconf, settings and imports.

提交回复
热议问题