Hey Ben.
I know it's a little bit late. But hope it helps.
-- The only gotcha I had is that the password needs to be hashed.
I ended up doing this to create a superuser:
I ended up doing this to create a superuser:
from django.db import migrationsfrom 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='admin@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),]
El martes, 14 de octubre de 2014, 19:43:38 (UTC-3), BenW escribió:
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!
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+unsubscribe@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/121c351a-920a-4ad7-a49a-90deea5eac08%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment