Django 1.7 data migration for custom user model

2019-02-16 02:53发布

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'

2条回答
姐就是有狂的资本
2楼-- · 2019-02-16 03:13

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'),
    ]
查看更多
做个烂人
3楼-- · 2019-02-16 03:25

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'

查看更多
登录 后发表回答