我通过电子邮件覆盖默认模型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'
应用程序.account.admin.py 并在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)