简体   繁体   中英

Django admin : extending the existing user model. doesn't show in admin site

I'm a very beginner to Django.

Using: Django 2.2.6 with python 3.6.3

Here is my problems with extending the existing user model. I've read this https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#extending-the-existing-user-model what I wanted to do is to

  1. use default auth.
  2. extends user model.
  3. see/manage the model I've extended in /admin

However I cannot see what I extended in /admin. I've done the following:

user.models.py:

from django.db import models
from django.contrib.auth.models import User

class UserInfo(models.Model):
    user = models.OneToOneField(User, unique = True, verbose_name = '學號', on_delete = models.CASCADE)
    sNickName = models.CharField(max_length = 16, verbose_name = "暱稱")
    iArticleNumber = models.PositiveIntegerField(verbose_name = "文章數")  # from 0 to 2,147,483,647
    sShortIntroduction = models.TextField(verbose_name = "短自介")

create an one to one model refer to User

user.admin.py:

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

from user.models import UserInfo

class UserInfoInline(admin.StackedInline):
    model = UserInfo
    can_delete = False
    verbose_name_plural = 'UserInfo'

class UserAdmin(BaseUserAdmin):
    inline = [UserInfoInline, ]

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

add UserInfo in User

And my /admin show like this: This page is the same as I did not add changes in user.admin.py. cannot find 'UserInfo' here.

What I expect is when I click specific user, like '410431135' in picture, I can see and manage the 'UserInfo', which I created to extends auth.models.User

Note: I've added some data in a specific user.userinfo by shell

Thanks. first time ask question here.

try these changes in admin.py file

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

class UserInfoInline(admin.StackedInline):
    model = UserInfo
    can_delete = False
    verbose_name_plural = 'UserInfo'

class UserAdmin(UserAdmin):
    inlines = (UserInfoInline,)
    list_display = ('username', 'first_name', 'last_name','UserInfo')

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

inlines

class UserAdmin(BaseUserAdmin):
    inlines = [UserInfoInline, ]

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