我将最新的Django用于Intranet项目。好吧,我按照django文档扩展了我的模型:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 0,
"hits": []
},
"aggregations": {
"myterms": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "john",
"doc_count": 2,
"count": {
"value": 1
},
"visit_date": {
"buckets": [
{
"key_as_string": "2018-08-01",
"key": 1533081600000,
"doc_count": 2
}
]
}
},
{
"key": "jane",
"doc_count": 1,
"count": {
"value": 1
},
"visit_date": {
"buckets": [
{
"key_as_string": "2018-08-01",
"key": 1533081600000,
"doc_count": 1
}
]
}
},
{
"key": "rob",
"doc_count": 1,
"count": {
"value": 1
},
"visit_date": {
"buckets": [
{
"key_as_string": "2018-08-01",
"key": 1533081600000,
"doc_count": 1
}
]
}
}
]
}
}
}
之后,我重写了admin.py:
from django.db import models
from django.contrib.auth.models import AbstractUser
class Employee(AbstractUser):
DEPARTMENTS = (
('ARE', 'Area Manager'),
('IT', 'IT'),
('CAT', 'Category manager'),
('CON', 'Controling')
)
departement = models.CharField(max_length = 3, verbose_name = "Département", choices = DEPARTMENTS)
正如您在admin.py的最后3行中看到的那样,如果我注册了用户模型,则会出错。如果我评论我的最后几行
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from .models import Employee
# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'Employees'
# Define a new User admin
class UserAdmin(BaseUserAdmin):
inlines = (EmployeeInline,)
# Re-register UserAdmin
# admin.site.register(User) # return error: django.contrib.admin.sites.NotRegistered: The model User is not registered
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
答案 0 :(得分:0)
您的问题是您的项目具有自定义模型用户定义。在这种情况下,建议的获取用户类的方法是使用get_user_model()
中的django.contrib.auth
。