我是Django的初学者。
使用: 带有Python 3.6.3的Django 2.2.6
这是扩展现有用户模型的问题。 我已经读过这个https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#extending-the-existing-user-model 我想做的就是
但是,我看不到/ admin中扩展的内容。 我已完成以下操作:
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 = "短自介")
创建一个与用户一对一的模型
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)
在用户中添加UserInfo
我的/ admin显示如下: This page is the same as I did not add changes in user.admin.py. cannot find 'UserInfo' here.
我希望当我单击特定用户时,例如图片中的“ 410431135”, 我可以看到和管理“ UserInfo”,它是我为扩展auth.models.User
而创建的注意: 我已经通过shell在特定的user.userinfo中添加了一些数据
谢谢!第一次在这里问问题。
答案 0 :(得分:2)
尝试在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)
答案 1 :(得分:1)
内联
const resp = {
data: {
id: '1',
displayName: 'A',
children: [{
id: '2',
displayName: 'B',
children: [{
id: '3',
displayName: 'C',
children: [
//More nested array here
]
}]
}]
}
}
const newKeys = {
displayName: "label"
};
const renamedObj = this.renameKeys(resp.data, newKeys);
console.log(renamedObj);
function renameKeys(obj, newKeys) {
const keyValues = Object.keys(obj).map(key => {
let newKey = null
if (key === 'displayName') {
newKey = newKeys.displayName
} else {
newKey = key
}
if (key === 'children') {
obj[key] = obj[key].map(obj => renameKeys(obj, newKeys));
}
return {
[newKey]: obj[key]
};
});
return Object.assign({}, ...keyValues);
}