Django 1.7 data migration for custom user model

后端 未结 2 1956
南笙
南笙 2021-01-07 15:21

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 p

相关标签:
2条回答
  • 2021-01-07 15:28

    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'),
        ]
    
    0 讨论(0)
  • 2021-01-07 15:47

    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'

    0 讨论(0)
提交回复
热议问题