Django和Python错误:CustomUserAdmin没有属性“_Meta”

zrfyljdw  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(110)

我正在为初学者开发Django,我有以下错误消息。
追溯消息:

(.venv) andrewstribling@Andrews-MBP news % python manage.py makemigrations accounts
Traceback (most recent call last):
  File "/Users/andrewstribling/Desktop/code/news/manage.py", line 22, in <module>
    main()
  File "/Users/andrewstribling/Desktop/code/news/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/core/management/__init__.py", line 416, in execute
    django.setup()
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/apps/registry.py", line 124, in populate
    app_config.ready()
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/contrib/admin/apps.py", line 27, in ready
    self.module.autodiscover()
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/contrib/admin/__init__.py", line 50, in autodiscover
    autodiscover_modules("admin", register_to=site)
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/utils/module_loading.py", line 58, in autodiscover_modules
    import_module("%s.%s" % (app_config.name, module_to_search))
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/andrewstribling/Desktop/code/news/accounts/admin.py", line 27, in <module>
    admin.site.register((CustomUser, CustomUserAdmin))
  File "/Users/andrewstribling/Library/Python/3.9/lib/python/site-packages/django/contrib/admin/sites.py", line 118, in register
    if model._meta.abstract:
AttributeError: type object 'CustomUserAdmin' has no attribute '_meta'

字符串
回溯的最后一行说我的类customuser admin说attribute没有Meta。我认为这是因为没有为Meta指定类。那么我只需要创建一个Meta类吗?
admin.py

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

""" The final step is to update our admin.py file since the admin is tightly coupled to the default
User model. We will extend the existing UserAdmin class to use our new CustomUser model.
To control which fields are listed, we use list_display. But to actually edit new custom fields,
like age, we must add fieldsets. And to include a new custom field in the section for creating a
new user we rely on add_fieldsets.""" 
# 
# Register your models here.
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = [
        "email",
        "username",
        "age",
        "is_staff",
    ]
    fieldsets = UserAdmin.fieldsets + ((None, {"fields": ("age",)}),)
    add_fieldsets = UserAdmin.add_fieldsets + ((None, {"fields": ("age",)}),)

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


我重新看了一下说明书。我查看了Stack Overflow文章。我在Discord上给人们发信息
我正在学习如何在Django中创建一个Meta类。我正在搜索Django论坛以获取更多信息。

aurhwmvo

aurhwmvo1#

你可以用**.register(…)**[Django-doc]将项目注册为两个参数,而不是一个二元组的单个参数,所以:

admin.site.register(CustomUser, CustomUserAdmin)

字符串

相关问题