Migrating old users into custom user model

33 views
Skip to first unread message

Ben Wattie

unread,
Aug 25, 2015, 5:35:51 PM8/25/15
to Django users
Hello,

I have an existing project with migrations, datamigrations and I need a custom user model now, not just 1-1 profile so I created one.

However, when I switch the AUTH_USER_MODEL setting, I can't access the old auth.user model for my datamigration

def transfer_users(apps, schema_editor):
    OldUser = apps.get_model('auth', 'User')
    User = apps.get_model('users', 'User')
    for old_user in OldUser.objects.all():
        new_user = User.objects.create(
            date_joined=old_user.date_joined,
            email=old_user.email and old_user.email or '%s...@example.com' % old_user.username,
            first_name=old_user.first_name,
            id=old_user.id,
            is_active=old_user.is_active,
            is_staff=old_user.is_staff,
            is_superuser=old_user.is_superuser,
            last_login=old_user.last_login,
            last_name=old_user.last_name,
            password=old_user.password)
        for perm in old_user.user_permissions.all():
            new_user.user_permissions.add(perm)
        for group in old_user.groups.all():
            new_user.groups.add(group)


class Migration(migrations.Migration):

    dependencies = [
        ('users', '0001_initial'),
        ('auth', '0006_require_contenttypes_0002'),
    ]

    operations = [
        migrations.RunPython(transfer_users)
    ]


Running this migration gives 
AttributeError: Manager isn't available; User has been swapped for 'users.User'
When trying to use OldUser.objects.all()

Is there another way to do this? Preferably in a datamigration not using a database shell


Carl Meyer

unread,
Aug 25, 2015, 5:59:37 PM8/25/15
to django...@googlegroups.com
Hi Ben,

On 08/25/2015 03:33 PM, Ben Wattie wrote:
> I have an existing project with migrations, datamigrations and I need a
> custom user model now, not just 1-1 profile so I created one.

Unfortunately, while this is technically possible (I haven't done it
myself, but I know at least one person who says they have), it's very
complex and requires a fair amount of familiarity with the migrations
system and use of the advanced SeparateDatabaseAndState operation. And
nobody, AFAIK, has written documentation on how to do it.

I don't believe this is really an acceptable state of affairs, so I've
filed https://code.djangoproject.com/ticket/25313 to at least clearly
acknowledge that it _ought_ to be fixed. I don't know when I'll get a
chance to work on it myself (not real likely unless I revisit an old
project, because nowadays I _always_ start every project with a custom
User model, to avoid this problem). Maybe the ideal scenario here is
that you figure it out and then write the docs on how it's done :-)

Sorry I can't give you a better answer,

Carl

signature.asc

Ben Wattie

unread,
Aug 25, 2015, 7:14:18 PM8/25/15
to Django users
Hi Carl, 

Thanks for your reply.
If I figure it out I would surely submit doc, but it's more likely that I will just come up with a workaround.

Starting a project with a custom user model every time just to avoid this doesn't feel so great since the documentation just says things like  "probably don't use a custom user model, even though its supported, use 1-1 profile instead"
But then later on the track requirements can change and you need a custom model but its very difficult to migrate.

Carl Meyer

unread,
Aug 25, 2015, 7:16:58 PM8/25/15
to django...@googlegroups.com
Hi Ben,

On 08/25/2015 05:14 PM, Ben Wattie wrote:
> Thanks for your reply.
> If I figure it out I would surely submit doc, but it's more likely that
> I will just come up with a workaround.

Fair enough :-)

> Starting a project with a custom user model every time just to avoid
> this doesn't feel so great since the documentation just says things like
> "probably don't use a custom user model, even though its supported, use
> 1-1 profile instead"
> But then later on the track requirements can change and you need a
> custom model but its very difficult to migrate.

And that's why the docs are simply wrong when they say that, and should
be changed. See https://code.djangoproject.com/ticket/24370

Carl

signature.asc
Reply all
Reply to author
Forward
0 new messages