Django Custom User — Edit new CustomUser fields in admin

笑着哭i 提交于 2020-02-26 06:36:20

问题


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

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

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:

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

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.

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

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:

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')

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!


回答1:


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

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



回答2:


Andy try adding this to your admin class:

fieldsets = (
        (('User'), {'fields': ('username', 'email','is_staff', 'bio')}),
    )

You can also add other sets for example another section that is all about permissions, and can display information about is_active, or groups. You can do this:

fieldsets = (
        (('User'), {'fields': ('username', 'email','is_staff', 'bio')}),
        (('Permissions'), {'fields': ('is_active','is_staff')}),
    )

You can just insert fieldsets underneath list_display. There is also a readonly_fields for fields that you do not want to be editable in the admin.



来源:https://stackoverflow.com/questions/53355638/django-custom-user-edit-new-customuser-fields-in-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!