且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将现有的auth.User数据迁移到新的Django 1.5自定义用户模型?

更新时间:2023-09-15 12:23:16

为你做这个迁移,但你需要聪明才能分阶段进行。以下是分步指南:(本指南预先假定您为 AbstractUser 而不是 AbstractBaseUser

South is more than able to do this migration for you, but you need to be smart and do it in stages. Here's the step-by-step guide: (This guide presupposed you subclass AbstractUser, not AbstractBaseUser)


  1. 在进行切换之前,请确保在包含您的自定义用户模型的应用程序
    中启用了南方支持的指南,我们将其称为帐户和模型用户)。
    此时您应该 有自定义用户模式。

  1. Before making the switch, make sure that south support is enabled in the application that contains your custom user model (for the sake of the guide, we'll call it accounts and the model User). At this point you should not yet have a custom user model.

$ ./manage.py schemamigration accounts --initial
Creating migrations directory at 'accounts/migrations'...
Creating __init__.py in 'accounts/migrations'...
Created 0001_initial.py.

$ ./manage.py migrate accounts [--fake if you've already syncdb'd this app]
 Running migrations for accounts:
 - Migrating forwards to 0001_initial.
 > accounts:0001_initial
 - Loading initial data for accounts.


  • 在帐户应用中创建一个新的空白用户迁移。

  • Create a new, blank user migration in the accounts app.

    $ ./manage.py schemamigration accounts --empty switch_to_custom_user
    Created 0002_switch_to_custom_user.py.
    


  • 创建您的自定义用户帐户应用程序中,但请确保其定义为:

  • Create your custom User model in the accounts app, but make sure it is defined as:

    class SiteUser(AbstractUser): pass
    


  • 使用以下代码填写空白迁移。 / p>

  • Fill in the blank migration with the following code.

    # encoding: utf-8
    from south.db import db
    from south.v2 import SchemaMigration
    
    class Migration(SchemaMigration):
    
        def forwards(self, orm):
            # Fill in the destination name with the table name of your model
            db.rename_table('auth_user', 'accounts_user')
            db.rename_table('auth_user_groups', 'accounts_user_groups')
            db.rename_table('auth_user_user_permissions', 'accounts_user_user_permissions')
    
        def backwards(self, orm):
            db.rename_table('accounts_user', 'auth_user')
            db.rename_table('accounts_user_groups', 'auth_user_groups')
            db.rename_table('accounts_user_user_permissions', 'auth_user_user_permissions')
    
        models = { ....... } # Leave this alone
    


  • 运行迁移

  • Run the migration

    $ ./manage.py migrate accounts
     - Migrating forwards to 0002_switch_to_custom_user.
     > accounts:0002_switch_to_custom_user
     - Loading initial data for accounts.
    


  • 现在对您的用户模型进行任何更改。

  • Make any changes to your user model now.

    # settings.py
    AUTH_USER_MODEL = 'accounts.User'
    
    # accounts/models.py
    class SiteUser(AbstractUser):
        site = models.ForeignKey(Site, null=True)
    


  • p>为此更改创建并运行迁移

  • create and run migrations for this change

    $ ./manage.py schemamigration accounts --auto
     + Added field site on accounts.User
    Created 0003_auto__add_field_user_site.py.
    
    $ ./manage.py migrate accounts
     - Migrating forwards to 0003_auto__add_field_user_site.
     > accounts:0003_auto__add_field_user_site
     - Loading initial data for accounts.
    


  • 老实说,如果你已经有了很好的知识您的设置已经使用南方,应该像在帐户模块中添加以下迁移一样简单。

    Honestly, If you already have good knowledge of your setup and already use south, It should be as simple as adding the following migration to your accounts module.

    # encoding: utf-8
    from south.db import db
    from south.v2 import SchemaMigration
    from django.db import models
    
    class Migration(SchemaMigration):
    
        def forwards(self, orm):
            # Fill in the destination name with the table name of your model
            db.rename_table('auth_user', 'accounts_user')
            db.rename_table('auth_user_groups', 'accounts_user_groups')
            db.rename_table('auth_user_permissions', 'accounts_user_permissions')
            # == YOUR CUSTOM COLUMNS ==
            db.add_column('accounts_user', 'site_id',
                models.ForeignKey(orm['sites.Site'], null=True, blank=False)))
    
        def backwards(self, orm):
            db.rename_table('accounts_user', 'auth_user')
            db.rename_table('accounts_user_groups', 'auth_user_groups')
            db.rename_table('accounts_user_user_permissions', 'auth_user_user_permissions')
            # == YOUR CUSTOM COLUMNS ==
            db.remove_column('accounts_user', 'site_id')
    
        models = { ....... } # Leave this alone
    

    编辑2/5/13:为auth_user_group表添加重命名。由于数据库约束,FK将自动更新为指向正确的表,但M2M字段的表名称是从两个表的名称生成的,需要以这种方式手动更新。

    EDIT 2/5/13: added rename for auth_user_group table. FKs will auto update to point at the correct table due to db constraints, but M2M fields' table names are generated from the names of the 2 end tables and will need manual updating in this manner.

    EDIT 2:感谢@Tuttle& @ pix0r进行更正。

    EDIT 2: Thanks to @Tuttle & @pix0r for the corrections.