Django 1.7 data migration for custom user model

故事扮演 提交于 2019-11-30 20:58:54

问题


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'

回答1:


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'




回答2:


I solved this issue using below workaround,

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


来源:https://stackoverflow.com/questions/30661704/django-1-7-data-migration-for-custom-user-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!