我是 Python/Django 的新手,我被要求将旧项目从 Python 3.7 / Django 2.2.5 迁移到 Python 3.12 和 Django 5.1,但是当我这样做时,一些功能现在不起作用。
例如,在我之前在管理界面中单击“添加观察者”时,我可以创建观察者并在同一页面中选择相关的触发器、指示器和其他人员。
但是在我的新版本中,当我选择“添加观察者”时,出现此错误:
RuntimeError at /admin/watchers/watcher/add/
'empty_form' is not used in polymorphic formsets, use 'empty_forms' instead.
我正在使用 django-polymorphic、nested-admin 的最新版本
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin, PolymorphicInlineSupportMixin
import nested_admin
from django.db import transaction
from watchers.models import *
class TriggerInline(nested_admin.NestedStackedPolymorphicInline):
model = apps.get_model('triggers', 'Trigger')
child_inlines = tuple([type(f'{subclass.__name__}Inline', (nested_admin.NestedStackedPolymorphicInline.Child,),
{
'model': subclass,
'inlines': [
TriggerComponentInline] if subclass.__name__ == "CompositeTrigger" else []
}) for subclass in apps.get_model('triggers', 'Trigger').__subclasses__()])
#Same that TriggerInline
class IndicatorInline(nested_admin.NestedStackedPolymorphicInline)
class WatcherChildAdmin(PolymorphicChildModelAdmin):
base_model = Watcher
inlines = (IndicatorInline, TriggerInline,)
#Other infos
#Register subclass
for subclass in Watcher.__subclasses__():
admin_class = type(f'{subclass.__name__}Admin',
(nested_admin.NestedPolymorphicInlineSupportMixin,WatcherChildAdmin,),
{
'base_model': subclass,
'exclude': ['periodicTask', ],
})
admin.site.register(subclass, admin_class)
@admin.register(Watcher)
class WatcherParentAdmin(PolymorphicInlineSupportMixin, PolymorphicParentModelAdmin):
base_model = Watcher
child_models = tuple(Watcher.__subclasses__())
#Other Functions
触发器和指示器在模型中都有观察者,例如:
类指标(多态模型): …… watcher = models.OneToOneField(Watcher, null=True, on_delete=models.CASCADE)
如果你帮助我,你比 Chatgpt 更好:)
完整追溯=>
`Environment:
Request Method: GET
Request URL: http://localhost:7000/admin/watchers/watcher/add/?ct_id=24
Django Version: 5.1
Python Version: 3.12.5
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'indicators',
'triggers',
'watchers',
'underlyings',
'users',
'frontend',
'rest_framework',
'polymorphic',
'django_celery_beat',
'webpush',
'corsheaders',
'taggit',
'taggit_serializer',
'phone_field',
'ckeditor',
'ckeditor_uploader',
'nested_admin']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.gzip.GZipMiddleware',
'corsheaders.middleware.CorsMiddleware']
Traceback (most recent call last):
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\contrib\admin\options.py", line 718, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper
result = _process_exception(request, e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\views\decorators\cache.py", line 80, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\contrib\admin\sites.py", line 241, in inner
return view(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\polymorphic\admin\parentadmin.py", line 211, in add_view
return real_admin.add_view(request, form_url, extra_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\contrib\admin\options.py", line 1961, in add_view
return self.changeform_view(request, None, form_url, extra_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\utils\decorators.py", line 48, in _wrapper
return bound_method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper
result = _process_exception(request, e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\contrib\admin\options.py", line 1820, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\contrib\admin\options.py", line 1919, in _changeform_view
media += inline_formset.media
^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\contrib\admin\helpers.py", line 466, in media
for fs in self:
^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\django\contrib\admin\helpers.py", line 376, in __iter__
self.formset.empty_form,
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\orion\venv\Lib\site-packages\polymorphic\formsets\models.py", line 292, in empty_form
raise RuntimeError(
^
Exception Type: RuntimeError at /admin/watchers/watcher/add/
Exception Value: 'empty_form' is not used in polymorphic formsets, use 'empty_forms' instead.
`
谢谢
django-polymorphic 被列为仅支持 Django 版本 2.2、3.0、3.1、3.2 和 4.0。它不支持 Django 版本 5.1。