Home > Net >  Is it possible to add more fields to admin/auth/user?
Is it possible to add more fields to admin/auth/user?

Time:12-27

models.py

class UserProfile(User):
    bio = models.TextField(blank=True, null=True)
    pfp = models.ImageField(verbose_name='Profile Picture', blank=True, null=True, upload_to="images/profile")

forms.py

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField(widget=forms.EmailInput(attrs={"class":"form-control", "placeholder":"[email protected]"}))
    first_name = forms.CharField(widget=forms.TextInput(attrs={"class":"form-control"}))
    last_name = forms.CharField(widget=forms.TextInput(attrs={"class":"form-control"}))
    pfp = forms.ImageField(required=False, widget=forms.FileInput(attrs={"class":"form-control"}))
    bio = forms.CharField(required=False, widget=forms.Textarea(attrs={"class":"form-control", 'rows':5, "placeholder":"Write something about yourself..."}))
    
    class Meta:
        model = UserProfile
        fields = ['first_name', 'last_name', 'username', 'email', "pfp", "bio"]
          

When creating the user, the two newly added fields (bio and pfp) are not being saved in admin/auth/user, so my question is, is it possible to add those fields to the admin users database?

views.py

class SignUpView(CreateView):
    form_class = UserRegisterForm
    template_name = "registration/signup.html"
    success_url = reverse_lazy("login")

CodePudding user response:

are not being saved in admin/auth/user

Indeed, these are safed on the UserProfile model.

You thus can make a ModelAdmin for this:

# app_name/admin.py

from django.contrib import admin
from app_name.models import UserProfile


@admin.register(UserProfile)
class AuthorAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'username', 'email', 'pfp', 'bio')

Then the details can be seen in the admin/app_name/userprofile section.

CodePudding user response:

Create a custom user model by inheriting the AbstractUser and adding extra fields needed. After that, register that custom user model by assigning it to AUTH_USER_MODEL.

Check here for detailed implementation.

  • Related