Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Custom User --- Edit new CustomUser fields in admin

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!

like image 319
Andy Avatar asked Nov 17 '18 21:11

Andy


1 Answers

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

fieldsets = UserAdmin.fieldsets + (
    (None, {'fields': ('some_extra_data',)}),
)
like image 50
Brendan Metcalfe Avatar answered Sep 30 '22 17:09

Brendan Metcalfe