栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

South不仅可以为你完成此迁移,但你需要精明并分阶段进行。以下是分步指南:(此指南假设你是子类

AbstractUser
,而不是
AbstractbaseUser

  1. 进行切换之前,请确保在包含你的自定义用户模型的应用程序中启用了南方支持(为便于指导,我们将其称为
    accounts
    和模型
    User
    )。此时,你应该还没有自定义用户模型。
$ ./manage.py schemamigration accounts --initialCreating 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.
  1. 在帐户应用中创建一个新的空白用户迁移。
$ ./manage.py schemamigration accounts --empty switch_to_custom_userCreated 0002_switch_to_custom_user.py.
  1. User在accounts应用程序中创建你的自定义模型,但请确保将其定义为:
class SiteUser(AbstractUser): pass
  1. 使用以下代码填写空白迁移。
# encoding: utf-8from south.db import dbfrom south.v2 import SchemaMigrationclass 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
  1. 运行迁移
$ ./manage.py migrate accounts - Migrating forwards to 0002_switch_to_custom_user. > accounts:0002_switch_to_custom_user - Loading initial data for accounts.
  1. 现在对你的用户模型进行任何更改。
# settings.pyAUTH_USER_MODEL = 'accounts.User'# accounts/models.pyclass SiteUser(AbstractUser):    site = models.ForeignKey(Site, null=True)
  1. 为此更改创建并运行迁移
$ ./manage.py schemamigration accounts --auto + Added field site on accounts.UserCreated 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.

老实说,如果你已经对设置有所了解并且已经使用过South,那么这就像向帐户模块添加以下迁移操作一样简单。

# encoding: utf-8from south.db import dbfrom south.v2 import SchemaMigrationfrom django.db import modelsclass 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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/400421.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号