how to add custom user model to users section of admin panel - django 3.1.1

374 Views Asked by At

I have added a custom user model in my django project which was explained here.

I defined extra fields in models.py like this:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    company = models.CharField(max_length=25, blank=True)
    address = models.TextField(max_length=500, blank=True)
    zipcode = models.CharField(max_length=15, blank=True)
    city = models.CharField(max_length=25, blank=True)
    country = models.CharField(max_length=25, blank=True)
    phone = models.CharField(max_length=15)
    

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

in my views.py, the registration goes like this:

from django.shortcuts import render, redirect
from django.contrib.auth.models import User

def update_profile(request, user_id, company, address, zipcode, city, country, phonenumber):
    user = User.objects.get(pk=user_id)
    user.profile.company = company
    user.profile.address = address
    user.profile.zipcode = zipcode
    user.profile.city = city
    user.profile.country = country
    user.profile.phoneNumber = phonenumber
    user.save()

def register(request):
    if request.method == 'POST':
        # Get form values
        first_name = request.POST['firstname']
        last_name = request.POST['lastname']

        company = request.POST['company']
        address = request.POST['address']

        zipcode = request.POST['zipcode']
        city = request.POST['city']

        country = request.POST['country']
        phoneNumber = request.POST['phoneNumber']

        username = request.POST['username']
        email = request.POST['email']

        password = request.POST['password']
        password2 = request.POST['password2']

        # Check if password match
        if password == password2:
            # Check username
            if User.objects.filter(username=username).exists():
                return redirect('register')
            else:
                if User.objects.filter(email=email).exists():
                    return redirect('register')
                else: 
                    # looks good
                    user = User.objects.create_user(username=username, 
                                                    password=password,
                                                    email=email,
                                                    first_name=first_name,
                                                    last_name=last_name)


                    user.save()
                    update_profile(request, user.id, company, address, zipcode, city, country, phoneNumber)
                    return redirect('login')

        else:
            return redirect('register')
    else:
        return render(request, 'users/register.html')

now, in admin panel, the custom model should be added in a separate section like this:

from django.contrib import admin
from .models import Profile

admin.site.register(Profile)

enter image description here

How can I add the fields of custom user model (i.e. Profile) in users page under AUTHENTICATION AND AUTHORIZATION section?

0

There are 0 best solutions below