我不想破坏我网站上的所有用户。但是我想利用Django 1.5的自定义可插入用户模型。这是我的新用户模型:
class SiteUser(AbstractUser): site = models.ForeignKey(Site, null=True)
一切都可以在新安装的新模型上正常工作(我还有其他代码,并且有这样做的充分理由-所有这些在这里都无关紧要)。但是,如果我将其放在我的活动站点上并进行同步数据库和迁移,我将失去所有用户,或者至少他们将位于与为新模型创建的新表不同的孤立表中。
我对South很熟悉,但是根据这篇文章和我的一些试验,似乎它的数据迁移当前不适合这种特定的迁移。因此,我正在寻找某种方法来使South能够为此工作或进行一些非South迁移(原始SQL,dumpdata / loaddata或其他方式),以便可以在每台服务器上运行(Postgres 9.2)以迁移用户一旦创建了新表,而旧的auth.User表仍在数据库中。
South不仅可以为你完成此迁移,但你需要精明并分阶段进行。以下是分步指南:(此指南假设你是子类AbstractUser,而不是AbstractBaseUser)
AbstractUser
AbstractBaseUser
accounts
User
$ ./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.
$ ./manage.py schemamigration accounts --empty switch_to_custom_user Created 0002_switch_to_custom_user.py.
class SiteUser(AbstractUser): pass
# 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
$ ./manage.py migrate accounts - Migrating forwards to 0002_switch_to_custom_user. > accounts:0002_switch_to_custom_user - Loading initial data for accounts.
# settings.py AUTH_USER_MODEL = 'accounts.User' # accounts/models.py class SiteUser(AbstractUser): site = models.ForeignKey(Site, null=True)
$ ./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.
老实说,如果你已经对设置有所了解并且已经使用过South,那么这就像向帐户模块添加以下迁移操作一样简单。
# 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