简体   繁体   中英

Django - Extending user profile not showing fileds in chenge form

I want to extend the user model in django adding new fields and of course be able to manage it through the admin pages. I created a CustomUser model in django extending AbstractUser and added some extra fields, created a UserCustomCreateForm and UserCustomChangeForm to manage the creation and change and then registered a CustomUserAdmin that extend the UserAdmin adding the two forms.
In admin, it seems to work fine when create a new users but unfortunately the new fields don't appear on the change view.

What am i doing wrong?

Thanks

here is the models.py

 users/models.py
# Create your models here.

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


class CustomUser(AbstractUser):
    # add additional fields in here
    address = models.CharField(max_length=50, null=True, verbose_name='Indirizzo')
    house_phone = models.CharField(max_length=20, null=True, verbose_name='Telefono')
    mobile_phone = models.CharField(max_length=20, null=True, verbose_name='Cellulare')

    def __str__(self):
        return self.first_name + ' ' + self.last_name

here the forms


# users/forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
from django import forms

class UserCustomCreateForm(UserCreationForm):

    class Meta(UserCreationForm):
        model = CustomUser
        fields = ('username', 'email', 'first_name', 'last_name',
                  'address', 'house_phone', 'mobile_phone')


class UserCustomChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = (
                  'username', 'email', 'first_name', 'last_name',
                  'address', 'house_phone', 'mobile_phone')

And here the admin.py

# users/admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from .forms import UserCustomCreateForm, UserCustomChangeForm
from .models import CustomUser
from django.contrib.auth.forms import UserCreationForm, UserChangeForm


class CustomUserAdmin(UserAdmin):
    add_form = UserCustomCreateForm
    form = UserCustomChangeForm
    change_form = UserCustomChangeForm
    model = CustomUser

    add_fieldsets = (
     ( "Dati di Accesso", {
            'classes': ('wide',),
            'fields':('username', 'password1', 'password2',)
     }
     ),
     ( "Informazioni Personali", {
        'classes': ('wide',),
        'fields': ( 'email', 'first_name', 'last_name',
                  'address', 'house_phone', 'mobile_phone')
     } ),
    )
    list_display = ['email', 'username',]

admin.site.register(CustomUser, CustomUserAdmin)

In your code of admin.py file change add_fieldsets = [...] to fieldsets = [...] .

The fieldsets defines the fieldsets for both views (add, change). The add_fieldsets define the result only for the add view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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