我想在 django 1.7 项目的数据迁移中播种超级用户记录。我正在使用django-custom-user应用程序。我的迁移如下所示:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
def load_data(apps, schema_editor):
EmailUser = apps.get_model('custom_user', 'EmailUser')
root = EmailUser.objects.create_superuser(email='admin@somesite.com', password='supersecure')
class Migration(migrations.Migration):
dependencies = [
('accounts', '0001_initial'),
]
operations = [
migrations.RunPython(load_data)
]
运行时./manage.py migrate
出现以下错误:
Running migrations:
Applying accounts.0002_initial_data...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
self.apply_migration(migration, fake=fake)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
migration.apply(project_state, schema_editor)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 105, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/operations/special.py", line 117, in database_forwards
self.code(from_state.render(), schema_editor)
File "/Users/spoolphiz/work/somesite/accounts/migrations/0002_initial_data.py", line 14, in load_data
root = EmailUser.objects.create_superuser(email='admin@somesite.com', password='f00b@r')
AttributeError: 'Manager' object has no attribute 'create_superuser'
我看到EmailUserManager
确实有一个 create_superuser 方法。我是否错误地调用了经理的功能?
编辑:
我在这里看到同样的问题。复制所有代码真的是最好的解决方案吗?似乎那时我最好使用 RunSQL 并手动执行插入。