简体   繁体   English

“ django.contrib.admin.sites.NotRegistered:未注册模型用户”想要注册我的自定义用户时出现此错误。

[英]“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. 我将覆盖默认模型AbstractUse以通过电子邮件进行授权。

applications.account.models.py 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. applications.account.admin.py并在admin.py文件中使用我的“自定义”用户。 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. 如果您已将AUTH_USER_MODEL设置设置为自定义模式,则不应注册默认的User应用程序。 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 : django-hijack-admin文档中 ,有一个用于自定义管理员的部分:

You need to set HIJACK_REGISTER_ADMIN = False in settings.py 您需要在settings.py中设置HIJACK_REGISTER_ADMIN = False

For Custom User Admins, the admin class can be modified as follows: 对于自定义用户管理员,可以如下修改admin类:

# 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: 对于具有用户外键的模型,有一个mixin可以按以下方式使用:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在我的 django 管理员上注册一个模型,但我收到了这个错误 - I want to register a model on my django admin and i got this error 如何将自定义 Django 用户 model 注册到管理页面? - How do I register a custom Django user model to the admin page? 创建/编辑/删除用户时,django 管理面板中的 FOREIGN KEY 约束失败。 (使用自定义用户模型。) - FOREIGN KEY constraint failed in django admin panel when creating/editing/deleting a user. (Using custom user model.) 尝试注册新用户时出现错误 401。 在本地主机上工作正常(Django rest api) - Error 401 when trying to register a new user. Works fine on localhost (Django rest api) 使用管理员身份注册自定义用户 model - Register custom user model with admin auth 为什么“django.contrib.admin.sites”没有“注册”属性? - Why 'django.contrib.admin.sites' has no attribute 'register'? Django自定义admin.site.register + admin.site.unregister在第一个HTTP GET(Apache + mod WSGI)上发生冲突的AlreadyRegistered + NotRegistered - Django custom admin admin.site.register + admin.site.unregister conflicting AlreadyRegistered + NotRegistered on 1st HTTP GET (Apache + mod WSGI) 如何修复我的 Django model 以存储用户输入的多个字符串列表。 将来可以检索哪个 object? - How can I fix my Django model to store multiple lists of strings inputted by the user. Which can then be retrieved in the future as an object? Django 1.5上admin中的自定义用户模型 - Custom User Model in admin on Django 1.5 引用从 django.contrib.auth.models 导入用户使用的 model 时,任务作业出错 - Error from Tasks job when referencing a model that uses from django.contrib.auth.models import User
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM