Estoy aprendiendo sobre Django y Rest Framework, tengo un pequeño proyecto para practicar, pero tengo un error al intentar acceder a http://localhost:8000/admin: TypeError: el objeto 'módulo' no se puede suscribir.

Estos son los archivos de Python que creé:

administrador.py:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.translation import gettext as _

from . import models


class UserAdmin(BaseUserAdmin):
    ordering = ['id']
    list_display = ['email', 'name']
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        (_('Personal Info'), {'fields': ('name',)}),
        (
            _('Permissions'),
            {
                'fields': (
                    'is_active',
                    'is_staff',
                    'is_superuser',
                )
            }
        ),
        (_('Important dates'), {'fields': ('last_login',)}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2')
        }),
    )


admin.site.register(models.User, UserAdmin)

Este es el rastreo:

Traceback (most recent call last): File "/home/jesus/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request)

File "/home/jesus/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request)

File "/home/jesus/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func response = view_func(request, *args, **kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 407, in login return LoginView.as_view(**defaults)(request)

File "/home/jesus/.local/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view return self.dispatch(request, *args, **kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper return bound_method(*args, **kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper return view(request, *args, **kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper return bound_method(*args, **kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view response = view_func(request, *args, **kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper return bound_method(*args, **kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func response = view_func(request, *args, **kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/contrib/auth/views.py", line 63, in dispatch return super().dispatch(request, *args, **kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/views/generic/base.py", line 97, in dispatch return handler(request, *args, **kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/views/generic/edit.py", line 133, in get return self.render_to_response(self.get_context_data())

File "/home/jesus/.local/lib/python3.8/site-packages/django/contrib/auth/views.py", line 96, in get_context_data context = super().get_context_data(**kwargs)

File "/home/jesus/.local/lib/python3.8/site-packages/django/views/generic/edit.py", line 66, in get_context_data kwargs['form'] = self.get_form()

File "/home/jesus/.local/lib/python3.8/site-packages/django/views/generic/edit.py", line 33, in get_form return form_class(**self.get_form_kwargs())

File "/home/jesus/.local/lib/python3.8/site-packages/django/contrib/auth/forms.py", line 204, in init self.fields['username'].max_length = username_max_length

TypeError: 'module' object is not subscriptable

respuesta

¿Intentaste darle un nombre diferente a tu clase? La clase 'UserAdmin' que ha definido puede entrar en conflicto con 'django.contrib.auth.admin' - UserAdmin.