简体   繁体   English

Django admin:扩展现有用户 model。 不在管理站点中显示

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

I'm a very beginner to Django.我是 Django 的初学者。

Using: Django 2.2.6 with python 3.6.3使用:Django 2.2.6 和 python 3.6.3

Here is my problems with extending the existing user model.这是我扩展现有用户 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我读过这个https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#extending-the-existing-user-model我想做的是

  1. use default auth.使用默认身份验证。
  2. extends user model.扩展用户 model。
  3. see/manage the model I've extended in /admin查看/管理我在 /admin 中扩展的 model

However I cannot see what I extended in /admin.但是我看不到我在 /admin 中扩展的内容。 I've done the following:我做了以下事情:

user.models.py:用户模型.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创建一对一 model 参考用户

user.admin.py:用户.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.而我的/admin 显示是这样的:这个页面和我没有在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我期望的是,当我单击特定用户时,例如图片中的“410431135”,我可以查看和管理我创建的用于扩展 auth.models.User 的“UserInfo”

Note: I've added some data in a specific user.userinfo by shell注意:我通过 shell 在特定的 user.userinfo 中添加了一些数据

Thanks.谢谢。 first time ask question here.第一次在这里提问。

try these changes in admin.py file在 admin.py 文件中尝试这些更改

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, ]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM