Django and Python Error: has no attribute '_meta

I am working on Django for Beginners newsletter project and I have the following error message.
Traceback message:

(.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'

So the last line there in the traceback that says my class customuser admin says attribute has no meta. I think that makes since because there isn’t a class for Meta specified. So do I just create a Meta class?
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))

I am thinking that all I need to do is create a class Meta. Am I on the right track? Right now I am checking Django Forum and stack overflow for solutions.

Following line in your code is incorect:

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

Epected form is:

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

done in by a parentheses!