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

Анастасия Руденко picture Анастасия Руденко · Apr 24, 2017 · Viewed 7k times · Source

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)

Answer

Alasdair picture Alasdair · Apr 24, 2017

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)