首页 > 解决方案 > Adding fields for User in admin page in django

问题描述

This problem seems to be very simple, yet i can't find solution. I needed to extend my User model (add phone number) in django, and i picked the way of creating another model called UserInfo that is related 1to1 with User model. It works fine, the only problem is that I can't get the UserInfo fields (the phone number) to show up on User page in admin panel. What i tried:

from app.models import UserInfo
from django.contrib import admin
from django.contrib.auth.models import User

class UserInfoInline(admin.TabularInline):
    model = UserInfo
class UserAdmin(admin.ModelAdmin):
    inlines = [UserInfoInline,]

admin.site.register(UserInfo)

EDIT: The current situation is:

try:
    admin.site.unregister(User)
except admin.sites.NotRegistered:
    pass
try:
    admin.site.register(User, UserAdmin)
except:
    pass

yet i still get the errors, this time from the sole django module:

python3.6/site-packages/django/contrib/admin/sites.py", line 109, in register
    raise AlreadyRegistered('The model %s is already registered' % model.__name__)
django.contrib.admin.sites.AlreadyRegistered: The model User is already registered

标签: pythondjango

解决方案


您应该做的是取消注册默认用户并注册UserAdmin

from app.models import UserInfo
from django.contrib import admin
from django.contrib.auth.models import User

class UserInfoInline(admin.TabularInline):
    model = UserInfo
class UserAdmin(admin.ModelAdmin):
    inlines = [UserInfoInline,]

# below lines should be added
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

尝试删除用户信息的注册部分

编辑:尝试遵循这个可行的示例:

模型.py

from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
    STUDENT = 1
    TEACHER = 2
    SUPERVISOR = 3
    ROLE_CHOICES = (
        (STUDENT, 'Student'),
        (TEACHER, 'Teacher'),
        (SUPERVISOR, 'Supervisor'),
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    location = models.CharField(max_length=30, blank=True)
    birthdate = models.DateField(null=True, blank=True)
    role = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, null=True, blank=True)

    def __str__(self):  # __unicode__ for Python 2
        return self.user.username

@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
    instance.profile.save()

管理员.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from .models import Profile

class ProfileInline(admin.StackedInline):
    model = Profile
    can_delete = False
    verbose_name_plural = 'Profile'
    fk_name = 'user'

class CustomUserAdmin(UserAdmin):
    inlines = (ProfileInline, )

    def get_inline_instances(self, request, obj=None):
        if not obj:
            return list()
        return super(CustomUserAdmin, self).get_inline_instances(request, obj)


admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

你可以在这里阅读更多关于它的信息。


推荐阅读