在迁移中访问 django-custom-user manager 方法

Access django-custom-user manager method in migration

我想在 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 does have a create_superuser method。我是否错误地调用了经理的功能?

编辑:

我看到了same issue here。复制所有代码真的是最好的解决方案吗?似乎那时我最好使用 RunSQL 并手动执行插入。

不幸的是经理们 weren't serialized before Django 1.8 因此 AttributeError 你来到这里。

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

我在 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' 在 'makemigrations --empty' 之前。我在 RunPython 调用 add_data() 之前移动了这个操作,但仍然得到 AttributeError.