10

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)

2 Answers2

11

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)
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Yes, I added `AUTH_USER_MODEL = 'applications.account.model.User' `in my settings and then register custom user- `admin.site.register(MyUser, ProfileAdmin)` but the i get a new error `ValueError: too many values to unpack` – Анастасия Руденко Apr 24 '17 at 09:49
  • The setting should be `AUTH_USER_MODEL = '.`, i.e. `AUTH_USER_MODEL = 'account.User'`. If you ran `makemigrations` or `migrate` before setting `AUTH_USER_MODEL` correctly, then you will probably have to delete your migration files and drop the database, because they will have used the default `User` model. – Alasdair Apr 24 '17 at 10:04
  • I get it, but I can`t delete my migrations and drop db, because it`s a working site. – Анастасия Руденко Apr 24 '17 at 10:57
  • 1
    It's difficult to switch from the default user to a custom user after a project has been started. See the [note in the docs](https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#changing-to-a-custom-user-model-mid-project). – Alasdair Apr 24 '17 at 11:00
  • I found one more solution, to use EmailAuthBackEnd. – Анастасия Руденко Apr 24 '17 at 13:11
2

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')
nageeb
  • 2,002
  • 1
  • 13
  • 25