扩展AbstractUser

时间:2018-12-04 14:58:13

标签: python-3.x django-2.1

我目前正在关注本教程: https://wsvincent.com/django-custom-user-model-tutorial/

我能够使本教程的代码正常工作并登录到管理面板。我的数据库中已经有了其他自定义表,现在我想将其包含在管理面板中进行编辑/查看。

我试图创建一个新的django模型,该模型仅作为测试即可反映数据库中的模型,但出现了错误(最后发布)。 “ RoomAdmin”是我数据库中的一个表,其中包含有关我正在创建的游戏中的房间的信息。我现在只想将其添加到管理面板中,以便通过面板直接进行编辑。

我已阅读到此错误可能与注册Admin模型时我放置变量的顺序有关,但是无论我做什么都行不通。

Admin.py:

from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser, RoomAdmin

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = ['email', 'username',]

class RoomAdmin(UserAdmin):
    ordering = ['name']
    list_display = ['name']

admin.site.register(CustomUser, 
                    CustomUserAdmin)
admin.site.register(RoomAdmin, 
                    CustomUserAdmin)

Models.py:

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    # add additional fields in here

    def __str__(self):
        return self.email

class RoomAdmin(models.Model):
    # add additional fields in here

    def __str__(self):
        return self.name

错误:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0426AC00>
Traceback (most recent call last):
  File "C:\github\hopemud\penv\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\github\hopemud\penv\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\github\hopemud\penv\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "C:\github\hopemud\penv\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "C:\github\hopemud\penv\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\github\hopemud\penv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\github\hopemud\penv\lib\site-packages\django\apps\registry.py", line 120, in populate
    app_config.ready()
  File "C:\github\hopemud\penv\lib\site-packages\django\contrib\admin\apps.py", line 24, in ready
    self.module.autodiscover()
  File "C:\github\hopemud\penv\lib\site-packages\django\contrib\admin\__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "C:\github\hopemud\penv\lib\site-packages\django\utils\module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "C:\github\hopemud\penv\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\github\hopemud\hopeweb_dev\accounts\admin.py", line 20, in <module>
    admin.site.register(RoomAdmin, CustomUserAdmin)
  File "C:\github\hopemud\penv\lib\site-packages\django\contrib\admin\sites.py", line 102, in register
    for model in model_or_iterable:
TypeError: 'MediaDefiningClass' object is not iterable

我似乎找不到摆脱这种错误的方法,并且希望获得一些见识。

0 个答案:

没有答案