0

プロジェクトのカスタム ユーザー モデルを作成しました。auth.User の内容を新しいカスタム ユーザー テーブルにコピーするデータ移行ファイルを作成しました。正しく動作していないようです。それは以下のエラーを投げています、

from django.db import migrations, transaction

@transaction.atomic
def copy_old_users(apps, schema_editor):
    User = apps.get_model("auth", "User")
    CustomUser = apps.get_model("custom_auth", "User")
    fields = ['id', 'username', 'email', 'first_name', 'last_name',
          'is_staff', 'is_active', 'date_joined', 'is_superuser',
          'last_login', 'password']

for user in User.objects.all():
    custom_user = CustomUser()
    for field in fields:
        setattr(custom_user, field, getattr(user, field))

    custom_user.save()

    custom_user.groups.add(*user.groups.all())
    custom_user.user_permissions.add(*user.user_permissions.all())

class Migration(migrations.Migration):

    dependencies = [
    ('auth', '0001_initial'),
    ('custom_auth', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(copy_old_users),
    ]

を実行するpython manage.py migrateと、このエラーが発生し、移行が解除されます:

Applying custom_auth.0002_auto_20150605_0700...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 166, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/migration.py", line 113, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/operations/special.py", line 117, in database_forwards
    self.code(from_state.render(), schema_editor)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/transaction.py", line 394, in inner
    return func(*args, **kwargs)
  File "/Users/youngkbell/personal/workspace/test/mydjango/custom_auth/migrations/0002_auto_20150605_0700.py", line 13, in copy_old_users
    for user in User.objects.all():
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 230, in __get__
    self.model._meta.object_name, self.model._meta.swapped
AttributeError: Manager isn't available; User has been swapped for 'custom_auth.User'
4

2 に答える 2

3

AUTH_USER_MODEL = 'auth.User'この移行を実行する前に設定する必要がsettings.pyあり、この移行が完了したら、に更新settings.pyしますAUTH_USER_MODEL = 'custom_auth.User'

于 2015-07-31T04:35:50.793 に答える