Форма Джанго не распознает Атрибут модели - PullRequest
1 голос
/ 19 июня 2019

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

1 Ответ

1 голос
/ 19 июня 2019

Вы позволяете вашему Meta классу наследовать от UserCreationForm.Вы не должны этого делать.Возможно, вы захотите наследовать от Meta из UserCreationForm:

class CustomLeiterCreationForm(UserCreationForm):

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

То же самое относится к Meta в CustomLeiterChangeForm.

Более того, вы написали:

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

Но вы не должны регистрировать CustomLeiterCreationForm, ни CustomLeiterChangeForm.Вы должны сделать подкласс UserAdmin и соответствующим образом обновить его, например:

из django.contrib import admin из django.contrib.auth.admin import 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>)
...