简体   繁体   中英

“django.contrib.admin.sites.NotRegistered: The model User is not registered” I get this error when a want to register my Custom User.

I overwrite default model AbstractUse for authorization through email.

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 And use my Custom user in admin.py file. I tried to unregistered default user model and register custom.

# -*- 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)

If you have set the AUTH_USER_MODEL setting to your custom mode, then the default User app should not be registered. Therefore, you should remove the line that is trying to unregister the default User model:

admin.site.unregister(User)

In the django-hijack-admin documentation , there's a section for custom admins :

You need to set HIJACK_REGISTER_ADMIN = False in settings.py

For Custom User Admins, the admin class can be modified as follows:

# admin.py
from hijack_admin.admin import HijackUserAdminMixin

class MyUserAdmin(UserAdmin, HijackUserAdminMixin):
    list_display = (
        ...
        'hijack_field',  # Hijack button
    )

For models with a foreign key to User, which is there is a mixin that can be used as follows:

#admin.py
from django.contrib import Admin
from hijack_admin.admin import HijackRelatedAdminMixin

class MyCustomerAdmin(HijackRelatedAdminMixin, admin.ModelAdmin)
    list_display = ('user', 'hijack_field')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM