gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:27 25 4
gpt4 key购买 nike

我想在 django 1.7 项目的数据迁移中植入 super 用户记录。我正在使用 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 并手动执行插入。

最佳答案

我在 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。

关于python - 在迁移中访问 django-custom-user manager 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28788819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com