Django 表单无法识别模型属性

Django Form not recognising model Attribute

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'

这是我的 forms.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 时的堆栈跟踪: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

您让 Meta class 继承自 UserCreationForm。你不应该那样做。您可能希望继承 MetaUserCreationForm:

class CustomLeiterCreationForm(UserCreationForm):

    class Meta(<b>UserCreationForm.Meta</b>):
        model = Leiter
        fields = ('username', 'email')

CustomLeiterChangeForm中的Meta也是如此。

此外你写道:

admin.site.register(Leiter, <s>CustomLeiterCreationForm</s>)

但是您不应该注册 CustomLeiterCreationFormCustomLeiterChangeForm。您应该创建 UserAdmin 的子 class,并相应地更新它,例如:

来自 django.contrib 导入管理员 从 django.contrib.auth.admin 导入 UserAdmin

class <b>LeiterUserAdmin</b>(UserAdmin):
    add_form = CustomLeiterCreationForm
    form = CustomLeiterChangeForm
    model = Leiter
    # some fields defined on Leiter
    list_display = ['email', 'username',]

admin.site.register(Leiter, <b>LeiterUserAdmin</b>)