0

I am using a custom user model with an extra field 'uid'. While i am trying to create a new user from the admin page, the uid field is not showing up. I've created a custom user create and update form.please see the code below.

models.py

from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    uid = models.CharField(max_length=50)

forms.py

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

class CustomUserCreationForm(UserCreationForm):
    uid = forms.CharField(label = "uid")

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

    def save(self, commit=True):
        user = super(UserCreateForm, self).save(commit=False)
        user.uid = self.cleaned_data["uid"]
        if commit:
            user.save()
        return user

Did i miss something or am i doing it wrong? please help.

Edit: admin.py

from django.contrib import admin
from support.models import CustomUser
# Register your models here.
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = ['username','password','uid']

admin.site.register(CustomUser, CustomUserAdmin)
2
  • add your admin.py file to the question Commented May 9, 2019 at 7:47
  • @JPG. i've added admin.py above. Commented May 9, 2019 at 8:36

1 Answer 1

1

Don't know if it's the right way, changing the CustomUserAdmin as shown in this answer https://stackoverflow.com/a/45701173/9740712 made the extra field appear.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.