我为我的项目创建了一个自定义用户模型。我编写了一个数据迁移文件,将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
时,我收到此错误,
unning迁移:
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'
答案 0 :(得分:3)
在运行此迁移之前,您应在AUTH_USER_MODEL = 'auth.User'
中设置settings.py
,此迁移完成后,将settings.py
更新为AUTH_USER_MODEL = 'custom_auth.User'
答案 1 :(得分:0)
我使用以下解决方法解决了这个问题,
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('custom_auth', '0001_initial'),
]
operations = [
migrations.RunSQL('INSERT INTO custom_auth_user SELECT * FROM auth_user'),
migrations.RunSQL('INSERT INTO custom_auth_user_groups SELECT * FROM auth_user_groups'),
migrations.RunSQL('INSERT INTO custom_auth_user_user_permissions SELECT * FROM auth_user_user_permissions'),
]