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="t...@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="t...@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!

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/971f560e-8f0b-4c6d-8ff8-00df760cc170%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to