Using data migration to create initial users errors

2,089 views
Skip to first unread message

BenW

unread,
Oct 14, 2014, 6:43:38 PM10/14/14
to django...@googlegroups.com
I am trying to create initial users in Django 1.7 via a simple data migration like so:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models, migrations


def populate_initial_data(apps, schema_editor):

   
   
User = apps.get_model(settings.AUTH_USER_MODEL)
   
Group = apps.get_model('auth', 'Group')

    user
= User.objects.create_user("test", email="te...@test.com", password="test")
   
# AttributeError: 'Manager' object has no attribute 'create_user'

   
group = Group.objects.create(name="Test Group")
    user
.groups.add(group)


class Migration(migrations.Migration):

    dependencies
= [
       
('auth', '__first__')
   
]

    operations
= [
        migrations
.RunPython(populate_initial_data)
   
]

However the instance of User does not have a UserManager, but rather a vanilla Manager object?

Possibly related problem, if I instead retrieve User via get_user_model() then I get a type error attempting to add that user to a group, like so:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models, migrations


def populate_initial_data(apps, schema_editor):


   
User = get_user_model()
   
Group = apps.get_model('auth', 'Group')

    user
= User.objects.create_user("test", email="te...@test.com", password="test")
   
group = Group.objects.create(name="Test Group")
    user
.groups.add(group)
   
# TypeError: 'Group' instance expected, got <Group: Group object>


class Migration(migrations.Migration):

    dependencies
= [
       
('auth', '__first__')
   
]

    operations
= [
        migrations
.RunPython(populate_initial_data)
   
]

I'm trying to replace the functionality of fixtures, which has apparently been deprecated with apps that use migrations.


Thanks!

Collin Anderson

unread,
Oct 16, 2014, 5:19:43 PM10/16/14
to django...@googlegroups.com
Hi Ben,

Yes, the custom manager objects are not available within migrations. I bet you might be able to import the UserManager and stick it on the User model yourself.

Collin

Guillaume Cisco

unread,
Nov 28, 2014, 5:47:24 AM11/28/14
to django...@googlegroups.com
Did you find a way to use method create_user in a data migration?
I can't see a way to import the manager for using it.

Markus Holtermann

unread,
Nov 28, 2014, 7:41:46 AM11/28/14
to django...@googlegroups.com
Hey all together,

unfortunately there isn't a way to add users through their manager in RunPython yet. The only solution I can see so far, is doing it manually, e.g. doing what your `create_user` method does inside RunPython. There is a patch though #23822 [1] that needs some tests and docs, but hopefully makes it into 1.8.


/Markus

Matías Iturburu

unread,
Feb 21, 2015, 10:11:00 AM2/21/15
to django...@googlegroups.com
Hey Ben.

The only gotcha I had is that the password needs to be hashed. 
I ended up doing this to create a superuser:

from django.db import migrations
from django.contrib.auth.hashers import make_password


def create_admin_user(apps, schema_editor):
    User = apps.get_registered_model('auth', 'User')
    admin = User(
        username='admin',
        email='ad...@admin.com',
        password=make_password('cambiame'),
        is_superuser=True,
        is_staff=True
    )
    admin.save()
 
class Migration(migrations.Migration):

    dependencies = [
        ('auth', '0001_initial')
    ]

    operations = [
        migrations.RunPython(create_admin_user),
    ]

I know it's a little bit late. But hope it helps.

Tim Huang

unread,
Sep 5, 2017, 10:04:10 PM9/5/17
to Django users
My instinct tells me that there may be something wrong with the classmethod normalize_username in _create_user method. 

What Matias suggested works. 

Melvyn Sopacua

unread,
Sep 6, 2017, 12:42:55 AM9/6/17
to django...@googlegroups.com
This is an old thread about 1.7.
Things have improved since then:
https://docs.djangoproject.com/en/1.11/topics/migrations/#model-managers
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6a37ad93-42b1-4595-9839-72148f42829f%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Melvyn Sopacua

Tim Huang

unread,
Sep 7, 2017, 1:36:39 PM9/7/17
to Django users
Hi Melvyn,

I am lack of knowledge how to use model manager. Would you mind give a quick example? Thanks

- timh 

James Schneider

unread,
Sep 10, 2017, 12:31:45 AM9/10/17
to django...@googlegroups.com
There's a fairly substantial doc page for model managers:


-James
Reply all
Reply to author
Forward
0 new messages