Django自定义用户---在admin中编辑新的CustomUser字段 [英] Django Custom User --- Edit new CustomUser fields in admin

查看:135
本文介绍了Django自定义用户---在admin中编辑新的CustomUser字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展William Vincent的教程,发布如下:

I am trying to expand on the tutorial by William Vincent, posted below:

https://wsvincent.com/django-custom-user-model-tutorial/

我试图将新字段添加到我通过django.contrib.auth.models通过AbstractUser导入扩展的CustomerUser模型中:

I am trying to add new fields to the CustomerUser model I extended via the AbstractUser import from django.contrib.auth.models:

users / models.py:

users/models.py:

from django.db import models
from django.contrib.auth.models import AbstractUser, UserManager

class CustomUserManager(UserManager):
    pass

class CustomUser(AbstractUser):
    bio         = models.TextField(max_length=500, blank=True)

    objects = CustomUserManager()

    def __str__(self):
        return self.username

我在上面的模型中添加了'bio'字段,但是当我通过django管理门户访问用户时,在t中看不到新的'bio'字段此处带有django打包的默认管理字段:即:个人信息:名字,姓氏,电子邮件地址等。

I added the 'bio' field to the model above, but when I access a user via the django admin portal, I don't see the new 'bio' field in there with the default admin fields packaged with django: ie: Personal info: first name, last name, email address, etc.

我的CustomUser应用已注册到管理门户像这样(遵循上述教程):

My CustomUser app is registered to the admin portal like so (following the tutorial mentioned above):

作为对我自己的测试,我能够在list_display中成功显示bio字段(按预期显示空白)。重申一下,我的问题是单击编辑用户时无法更新此字段。好消息是django迁移了我新的 bio领域。

As a test for myself, I was able to display the bio field successfully (showing blank as expected) in my list_display. To reiterate, my problem is I have no way of updating this field when I click to edit a user. The good news is that django picked up the migration of my new 'bio' field.

from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = ['username', 'email','is_staff', 'bio']

admin.site.register(CustomUser, CustomUserAdmin)

我的猜测是我正在寻找的解决方案与编辑管理表单。这是我的用户应用程序中的内容(来自本教程)。

My guess is that the solution I am looking for has something to do with editing the admin forms. Here is what I have in my user app (from the tutorial).

users / forms.py:

users/forms.py:

from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser

class CustomUserCreationForm(UserCreationForm):

    class Meta(UserCreationForm):
        model = CustomUser
        fields = ('username', 'email')

class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = ('username', 'email')

诚然,我对上面的form.py文件中发生的事情不太了解。我怀疑这可能与我通过管理门户访问的实际用户编辑表单无关,所以我可能只需要弄清楚如何对默认的Django管理应用进行更改。

Admittedly, I don't have a great understanding of what is happening in the form.py file just above. I suspect that it might have nothing to do with the actual user edit form that I am accessing via the admin portal, so I might just need to figure out how to make changes to the default django admin app.

一如既往,非常感谢您的帮助!

As always, your help is much appreciated, thanks!

推荐答案

'fieldsets +'方法比必须再次写出所有默认字段要好得多。

The 'fieldsets +' approach is much better than having to write out all the default fields again.

fieldsets = UserAdmin.fieldsets + (
    (None, {'fields': ('some_extra_data',)}),
)

这篇关于Django自定义用户---在admin中编辑新的CustomUser字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆