6

我想在 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 并手动执行插入。

4

2 回答 2

4

我在 1.8.4 中看到了与 @cpury 相同的问题。我在我的管理器中设置了 use_in_migrations=True,但出乎意料的是,自定义管理器方法在使用 Model.objects.my_custom_method() 的数据迁移中不可用,出现上述相同的错误。我所做的解决方法是导入管理器并实例化它。那么问题是 self.model 是 None 因为它没有附加到模型上。所以我做了以下事情,它似乎工作,虽然我不明白为什么 use_in_migrations 标志不能像我预期的那样工作。

from appname.managers import MyManager
def add_data(apps, schema_editor):
    mgr = MyManager()
    mgr.model = apps.get_model('appname', 'Model')
    mgr.my_method_that_adds_data()

编辑:看起来尚未创建为模型设置自定义管理器的 AlterModelManagers 操作,并且它出现在以后的迁移中。我可能忘记在“makemigrations --empty”之前运行“makemigrations”。我在 RunPython 调用 add_data() 之前将此操作移动了,但仍然得到 AttributeError。

于 2015-09-03T00:19:27.550 回答
0

不幸的是,在 Django 1.8 之前没有对管理器进行序列化,因此AttributeError您来到了这里。

我建议您升级到Django==1.8b1并运行makemigration以序列化您的自定义用户管理器。否则,您将需要使用您的香草经理的create方法来模仿所做的事情UserManager.create_superuser

于 2015-03-01T03:03:10.960 回答