我通过电子邮件覆盖默认模型AbstractUse以进行授权。
applications.account.models.py
# -*- coding: utf-8 -*-
import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
class Meta(AbstractUser.Meta):
swappable = 'AUTH_USER_MODEL'
USERNAME_FIELD = 'email'
applications.account.admin.py 并在admin.py文件中使用我的自定义用户。 我尝试了未注册的默认用户模型并注册自定义。
# -*- coding: utf-8 -*-
from django.contrib import admin
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from modeltranslation.admin import TabbedTranslationAdmin
from applications.account.models import Profile, Phone, ShippingAddress, PartnerGC, Referral, Operation
from applications.account.models import User as MyUser
class UserInline(admin.StackedInline):
model = Profile
max_num = 1
can_delete = False
class OperationInline(admin.StackedInline):
model = Operation
can_delete = True
class PhoneInline(admin.StackedInline):
model = Phone
can_delete = True
class PartnerGCInline(admin.StackedInline):
model = PartnerGC
max_num = 1
can_delete = True
class ReferralInline(admin.StackedInline):
model = Referral
max_num = 1
can_delete = True
class ShippingAddressInline(admin.StackedInline):
model = ShippingAddress
can_delete = True
class ProfileAdmin(admin.ModelAdmin):
list_filter = ["date_joined"]
search_fields = ["=id", "first_name", 'last_name', "email"]
list_display = ('id', "email", 'first_name', 'last_name', 'date_joined',
'last_login')
actions = ['mark_active', 'mark_inactive']
inlines = [UserInline, PhoneInline, ShippingAddressInline, PartnerGCInline, ReferralInline, OperationInline]
def user_email(self, instance):
return instance.user.email
user_email.short_description = u"E-mail"
def user_active(self, instance):
return instance.user.is_active
user_active.short_description = u"Активен"
user_active.boolean = True
def user_staff(self, instance):
return instance.user.is_staff
user_staff.short_description = u"Персонал"
user_staff.boolean = True
def user_superuser(self, instance):
return instance.user.is_superuser
user_superuser.short_description = u"Администратор"
user_superuser.boolean = True
def user_date_joined(self, instance):
return instance.user.date_joined
user_date_joined.short_description = u"Дата регистрации"
def user_last_login(self, instance):
return instance.user.last_login
user_last_login.short_description = u"Последняя активность"
def mark_active(self, request, queryset):
queryset.update(user__is_active=True)
return HttpResponseRedirect('')
mark_active.short_description = u"Активировать выбранные профили"
def mark_inactive(self, request, queryset):
queryset.update(user__is_active=False)
return HttpResponseRedirect('')
admin.site.unregister(User)
admin.site.register(MyUser, ProfileAdmin)
答案 0 :(得分:5)
如果您已将AUTH_USER_MODEL
设置设置为自定义模式,则不应注册默认的User
应用。因此,您应该删除尝试取消注册默认用户模型的行:
admin.site.unregister(User)
答案 1 :(得分:1)
在django-hijack-admin documentation中,有一个用于custom admins的区域:
您需要在settings.py中设置HIJACK_REGISTER_ADMIN = False
对于自定义用户管理员,可以按以下方式修改管理员类:
# admin.py
from hijack_admin.admin import HijackUserAdminMixin
class MyUserAdmin(UserAdmin, HijackUserAdminMixin):
list_display = (
...
'hijack_field', # Hijack button
)
对于具有用户外键的模型,其中有一个mixin可以按以下方式使用:
#admin.py
from django.contrib import Admin
from hijack_admin.admin import HijackRelatedAdminMixin
class MyCustomerAdmin(HijackRelatedAdminMixin, admin.ModelAdmin)
list_display = ('user', 'hijack_field')