将现有的auth.User数据迁移到新的Django 1.5自定义用户模型? [英] Migrating existing auth.User data to new Django 1.5 custom user model?

查看:133
本文介绍了将现有的auth.User数据迁移到新的Django 1.5自定义用户模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想摧毁我网站上的所有用户。但是我想利用Django 1.5的自定义可插拔用户模型。这是我的新用户模型:

I'd prefer not to destroy all the users on my site. But I want to take advantage of Django 1.5's custom pluggable user model. Here's my new user model:

class SiteUser(AbstractUser):
    site = models.ForeignKey(Site, null=True)

一切都适用于我的新模型的新安装(我有其他代码,沿着有一个很好的理由这样做 - 所有这些在这里是无关紧要的)。但是,如果我把它放在我的实时网站上,并且syncdb&迁移,我会丢失所有的用户,或至少他们将在与我的新模型创建的新表格不同的孤立表中。

Everything works with my new model on a new install (I've got other code, along with a good reason for doing this--all of which are irrelevant here). But if I put this on my live site and syncdb & migrate, I'll lose all my users or at least they'll be in a different, orphaned table than the new table created for my new model.

我熟悉南方,但基于这篇文章和我的一些试验,似乎它的数据迁移目前不适合这种特定的迁移。所以我正在寻找一些方法来为南方工作,或者为了在我的每个服务器(Postgres 9.2)上运行的一些非南移(原始SQL,dumpdata / loaddata或其他))来迁移用户一旦新的表被创建,而旧的auth.User表仍然在数据库中。

I'm familiar with South, but based on this post and some trials on my part, it seems its data migrations are not currently a fit for this specific migration. So I'm looking for some way to either make South work for this or for some non-South migration (raw SQL, dumpdata/loaddata, or otherwise) that I can run on each of my servers (Postgres 9.2) to migrate the users once the new table has been created while the old auth.User table is still in the database.

推荐答案

为你做这个迁移,但你需要聪明才能分阶段进行。以下是分步指南:(本指南预先假定您为 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.

    这篇关于将现有的auth.User数据迁移到新的Django 1.5自定义用户模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆