django - 将自定义 QuerySet 添加到 UserModel 会导致 makemigrations 异常
问题描述
我想为我的用户模型创建一个自定义查询集。但是,我不能简单地使用objects = UserQuerySet.as_manager()
,因为标准 Django User 模型已经有一个自定义 UserManager。
我的代码很简单:
class UserQuerySet(MyBaseQuerySet):
def opted_out_method(self):
return
class User(AbstractUser):
objects = UserManager.from_queryset(UserQuerySet)()
# etc...
此代码有效,除非我这样做:
$manage.py makemigrations --dry-run
File "./manage.py", line 49, in <module>
execute_from_command_line(sys.argv)
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py", line 164, in handle
changes = autodetector.changes(
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/db/migrations/autodetector.py", line 43, in changes
changes = self._detect_changes(convert_apps, graph)
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/db/migrations/autodetector.py", line 129, in _detect_changes
self.new_apps = self.to_state.apps
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/db/migrations/state.py", line 210, in apps
return StateApps(self.real_apps, self.models)
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/db/migrations/state.py", line 273, in __init__
self.render_multiple([*models.values(), *self.real_models])
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/db/migrations/state.py", line 308, in render_multiple
model.render(self)
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/db/migrations/state.py", line 577, in render
body.update(self.construct_managers())
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/db/migrations/state.py", line 536, in construct_managers
as_manager, manager_path, qs_path, args, kwargs = manager.deconstruct()
File "/home/user/.cache/pypoetry/virtualenvs/django-app-4WxKU_E8-py3.8/lib/python3.8/site-packages/django/db/models/manager.py", line 61, in deconstruct
raise ValueError(
引发的异常是“找不到管理器。请注意,您需要从使用 'from_queryset()" 动态生成的管理器继承,这就是我正在做的。
有没有人遇到过这个?
我目前正在使用 Django 2.2,不确定这是否已在 3+ 中修复。
解决方案
推荐阅读
- cypress - Cypress.io:创建元素存在条件,没有错误“重试超时”?
- python - 由于坐标系不一致,虹膜重新网格化失败
- flutter - 我需要解决 FormatException: Unexpected character Flutter?
- docker - 更新 Dockerfile 中的依赖并创建镜像,无需重新下载前面提到的依赖
- python-3.x - 带引号的正则表达式模式失败
- ios - 当应用程序在 iOS 中终止时如何禁用 vpn 连接?
- datetime - 未指定年份时,DialogFlow 错误年份
- json - Hive/Athena 中的 JSON SerDe:将一个 JSON 对象变成多行?
- c++ - boost::polygon 布尔减法导致额外的行
- ios - Swift - 生成条形码错误“线程 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”