Django在我的自定义用户表单中似乎无法识别model
对象。
这是错误:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/base.py", line 361, in execute
self.check()
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/contrib/admin/checks.py", line 56, in check_admin_app
errors.extend(site.check(app_configs))
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 81, in check
if modeladmin.model._meta.app_config in app_configs:
AttributeError: 'CustomLeiterCreationForm' object has no attribute 'model'
(venv) christian@christian-Lenovo-YOGA:~/PycharmProjects/django-rr-protokoll-tool/rr_protokoll_tool$ python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/base.py", line 361, in execute
self.check()
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/contrib/admin/checks.py", line 56, in check_admin_app
errors.extend(site.check(app_configs))
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 81, in check
if modeladmin.model._meta.app_config in app_configs:
AttributeError: 'CustomLeiterCreationForm' object has no attribute 'model'
这是我的表格。py
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import Leiter
class CustomLeiterCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = Leiter
fields = ('username', 'email')
class CustomLeiterChangeForm(UserChangeForm):
class Meta(UserChangeForm):
model = Leiter
fields = ('username', 'email')
我哪里出错了?
编辑:
我有Meta
时的stacktrace如下:class Meta(UserCreationForm.Meta)
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/apps/registry.py", line 122, in populate
app_config.ready()
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/contrib/admin/apps.py", line 24, in ready
self.module.autodiscover()
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/rr_protokoll_tool/tool/admin.py", line 16, in <module>
admin.site.register(Leiter, CustomLeiterCreationForm)
File "/home/christian/PycharmProjects/django-rr-protokoll-tool/venv/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 124, in register
self._registry[model] = admin_class(model, self)
TypeError: object() takes no parameters
答案 0 :(得分:1)
您让Meta
类继承自UserCreationForm
。你不应该那样做。您可能要继承Meta
的{{1}}:
UserCreationForm
class CustomLeiterCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Leiter
fields = ('username', 'email')
中的Meta
也是如此。
您还写了:
CustomLeiterChangeForm
但是您不应注册admin.site.register(Leiter,
或CustomLeiterCreationForm)CustomLeiterCreationForm
。您应该制作CustomLeiterChangeForm
的子类,并进行相应的更新,例如:
从django.contrib.auth.admin导入UserAdmin
UserAdmin