auth User in data migration: AttributeError: type object 'User' has no attribute 'normalize_username'

1,535 views
Skip to first unread message

Павел Фролов

unread,
Dec 18, 2016, 5:25:31 AM12/18/16
to Django users
Hello everybody,

could somebody help me
I try to do data migration. Migration uses User from auth. Django 1.10.4

def create_superuser(apps, schema_editor):
    User = apps.get_model("auth", "User")
    User.objects.create_superuser(username='admin', password='admin123admin', email='')

class Migration(migrations.Migration):
    dependencies = [
        ('auth', '__latest__'),
    ]
    operations = [
        migrations.RunPython(create_superuser),
    ]

I got an error:
...
File "/Projects/django/src/testdb/migrations/0001_initial.py", line 10, in create_superuser
    User.objects.create_superuser(username='admin', password='admin123admin', email='')
  File "/Projects/django/venv/lib/python3.4/site-packages/django/contrib/auth/models.py", line 168, in create_superuser
    return self._create_user(username, email, password, **extra_fields)
  File "/Projects/django/venv/lib/python3.4/site-packages/django/contrib/auth/models.py", line 148, in _create_user
    username = self.model.normalize_username(username)
AttributeError: type object 'User' has no attribute 'normalize_username'

I use standard User in my project and I need perform data migrations.

thank you in advance
Pavel

Павел Фролов

unread,
Dec 18, 2016, 5:31:46 AM12/18/16
to Django users
I found information here https://groups.google.com/forum/#!searchin/django-users/auth$20user$20in$20migration|sort:relevance/django-users/Zz9rKT7GVM0/u6A4HzgDylkJ
but I want not use "patching"

I did following:

def create_superuser(apps, schema_editor):
    # fixme: User = apps.get_model("auth", "User")
    from django.contrib.auth.models import User
    User.objects.create_superuser(username='admin', password='admin123admin', email='')

It works

воскресенье, 18 декабря 2016 г., 14:25:31 UTC+4 пользователь Павел Фролов написал:

Tim Graham

unread,
Dec 18, 2016, 6:18:43 PM12/18/16
to Django users
I think this is another symptom of https://code.djangoproject.com/ticket/26445.
Reply all
Reply to author
Forward
0 new messages