Django 1.7数据迁移用于自定义用户模型 [英] Django 1.7 data migration for custom user model

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

问题描述

我已经为我的项目创建了一个自定义用户模型。我写了一个数据迁移文件,将auth.User的内容复制到我的新的自定义用户表。它似乎没有正常工作。它正在抛出低于错误,

 从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',
' user_objects.all()中的用户的


custom_user = CustomUser()
字段中的字段:
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),
]
/ pre>

当我运行 python manage.py migrate 时,我收到这个错误,
unning迁移:

 应用custom_auth.0002_auto_20150605_0700 ...追溯(最近的最后一次呼叫):
文件manage.py ,第10行,在< module>
execute_from_command_line(sys.argv)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py,行385,在execute_from_command_line
utility.execute()
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__ .py,第377行,执行
self.fetch_command(子命令).run_from_argv(self.argv)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /site-packages/django/core/management/base.py,第288行,在run_from_argv
self.execute(* args,** options .__ dict__)
文件/ Library / Frameworks / Python .framework / Versions / 2.7 / lib / python2.7 / site-packages / django / core / management / base.py,行338,执行
output = self.handle(* args,** options)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/migrate.py,第166行,手头e
executor.migrate(targets,plan,fake = options.get(fake,False))
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /site-packages/django/db/migrations/executor.py,第68行,迁移
self.apply_migration(migration,fake = fake)
文件/Library/Frameworks/Python.framework/版本/ 2.7 / lib / python2.7 / site-packages / django / db / migrations / executor.py,第102行,apply_migration
migration.apply(project_state,schema_editor)
文件/ Library /Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/migration.py,第113行,应用
operation.database_forwards(self.app_label,schema_editor ,project_state,new_state)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/operations/special.py,第117行,在database_forwards
self.code(from_state.render(),schema_editor)
文件/Library/Frameworks/Python.framework/Vers离子/ 2.7 / lib / python2.7 / site-packages / django / db / transaction.py,第394行,内部
返回func(* args,** kwargs)
文件/ Users /youngkbell/personal/workspace/test/mydjango/custom_auth/migrations/0002_auto_20150605_0700.py,第13行,在user_objects.all()中的用户的copy_old_users
中:
文件/ Library / Frameworks /Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py,第230行,__get__
self.model._meta.object_name,self.model ._meta.swapped
AttributeError:Manager不可用;用户已被替换为'custom_auth.User'


解决方案

你在运行此迁移之前,应在 settings.py 中设置 AUTH_USER_MODEL ='auth.User',此迁移完成后,更新 settings.py AUTH_USER_MODEL ='custom_auth.User'


I have created a custom user model for my project. I have written a data migration file to copy contents of auth.User to my new custom user table. It doesn't seem to work properly. It is throwing below error,

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),
    ]

When I run python manage.py migrate, I am getting this error, unning migrations:

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'

解决方案

You should set AUTH_USER_MODEL = 'auth.User' in settings.py before run this migrate and after this migrate finishes, update the settings.py to AUTH_USER_MODEL = 'custom_auth.User'

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

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