使用自定义用户模型和管理员
时遇到了一些麻烦# models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class Employee(AbstractUser):
class Meta:
verbose_name = 'Employee'
verbose_name_plural = 'Employees'
# settings.py
AUTH_USER_MODEL = 'myapp.Employee'
# admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from myapp.models import Employee
admin.site.register(Employee, UserAdmin)
当我尝试添加新用户时,它告诉我这个错误:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8050/admin/chaos/employee/add/
Django Version: 1.7.1
Python Version: 2.7.6
Installed Applications:
['admin_tools',
'admin_tools.theming',
'admin_tools.menu',
'admin_tools.dashboard',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'reversion',
'mptt',
'imperavi',
'django_mptt_admin',
'document',
'chaos']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
584. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
204. return view(request, *args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
76. return view(request, *args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
25. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
25. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
394. return func(*args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/contrib/auth/admin.py" in add_view
121. extra_context)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
1454. return self.changeform_view(request, None, form_url, extra_context)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
25. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
394. return func(*args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in changeform_view
1397. if form.is_valid():
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
162. return self.is_bound and not bool(self.errors)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in errors
154. self.full_clean()
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
353. self._clean_fields()
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in _clean_fields
371. value = getattr(self, 'clean_%s' % name)()
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/contrib/auth/forms.py" in clean_username
101. User._default_manager.get(username=username)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
92. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in get
351. num = len(clone)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in __len__
122. self._fetch_all()
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
966. self._result_cache = list(self.iterator())
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/query.py" in iterator
265. for row in compiler.results_iter():
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
700. for rows in self.execute_sql(MULTI):
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
786. cursor.execute(sql, params)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
81. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/vagrant/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /admin/chaos/employee/add/
Exception Value: relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
任何人都有同样的事情和任何提示来解决它?
我怀疑我还需要继承原始UserAdmin使用的UserCreationForm
和UserChangeForm
,但如果这是正确的方法,我老实说看不到使用自定义用户模型,而不是创建一对一模型(例如Profile
)来扩展原始用户。
由于 卢卡
答案 0 :(得分:-4)
美好的一天,卢克!
请查看django docs:https://docs.djangoproject.com/en/dev/topics/auth/customizing/
只需检查您使用的是哪种型号:CustomUser或Abstract Base 用户。 我可以看到,现在它是 AbstractUser 。