TypeError: UserAdmin() takes 1 positional argument but 2 were given
TypeError: UserAdmin() takes 1 positional argument but 2 were given
我正在创建一个抽象用户,我在其中遇到此错误,请解决它。
我想为 django 制作一个自定义用户模型。
错误
(venv) python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/apps/registry.py", line 122, in populate
app_config.ready()
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/contrib/admin/apps.py", line 24, in ready
self.module.autodiscover()
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/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.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/mraries/Documents/shift_api/shift/admin.py", line 24, in <module>
admin.site.register(User,UserAdmin)
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 134, in register
self._registry[model] = admin_class(model, self)
TypeError: UserAdmin() takes 1 positional argument but 2 were given
Admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.translation import gettext as _
from .models import User
from . import models
def UserAdmin(BaseUserAdmin):
ordering = ['id']
list_display = ['email', 'name']
fieldsets = (
(None, {'field':('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', 'password', 'password2')
}),
)
admin.site.register(User,UserAdmin)
Models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
# Create your models here.
class UserManger(BaseUserManager):
def create_user(self, email, password=None, **extra_field):
"""creates and save a new user"""
if not email:
raise ValueError("User must have an email address")
user= self.model(email=self.normalize_email(email), **extra_field)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password):
user = self.create_user(email, password)
user.is_superuser = True
user.is_staff = True
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=225, unique=True)
name = models.CharField(max_length=100)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
object = UserManger()
USERNAME_FIELD = 'email'
fhbuirefgbeuifbeuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuucndfbvffvbdhbvhvbbvubfufuifhfhuhdfdshfsdjhfjfhjdshfjkshfjcbjcbvfbv fvu ifuhvuihvufdg urf huhvfudv fuhfuf fdhf[13fifu=
UserAdmin
应该是 class 而不是函数。
class UserAdmin(BaseUserAdmin):
...
我正在创建一个抽象用户,我在其中遇到此错误,请解决它。 我想为 django 制作一个自定义用户模型。
错误
(venv) python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/apps/registry.py", line 122, in populate
app_config.ready()
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/contrib/admin/apps.py", line 24, in ready
self.module.autodiscover()
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/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.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/mraries/Documents/shift_api/shift/admin.py", line 24, in <module>
admin.site.register(User,UserAdmin)
File "/home/mraries/Documents/shift_api/venv/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 134, in register
self._registry[model] = admin_class(model, self)
TypeError: UserAdmin() takes 1 positional argument but 2 were given
Admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.translation import gettext as _
from .models import User
from . import models
def UserAdmin(BaseUserAdmin):
ordering = ['id']
list_display = ['email', 'name']
fieldsets = (
(None, {'field':('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', 'password', 'password2')
}),
)
admin.site.register(User,UserAdmin)
Models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
# Create your models here.
class UserManger(BaseUserManager):
def create_user(self, email, password=None, **extra_field):
"""creates and save a new user"""
if not email:
raise ValueError("User must have an email address")
user= self.model(email=self.normalize_email(email), **extra_field)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password):
user = self.create_user(email, password)
user.is_superuser = True
user.is_staff = True
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=225, unique=True)
name = models.CharField(max_length=100)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
object = UserManger()
USERNAME_FIELD = 'email'
fhbuirefgbeuifbeuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuucndfbvffvbdhbvhvbbvubfufuifhfhuhdfdshfsdjhfjfhjdshfjkshfjcbjcbvfbv fvu ifuhvuihvufdg urf huhvfudv fuhfuf fdhf[13fifu=
UserAdmin
应该是 class 而不是函数。
class UserAdmin(BaseUserAdmin):
...