the User Model wont update, after i try to change first_name and last_name from django admin

i extended the user model usng AbstractUser model and added proxy model to devided some user role. and the problem is i cant update the user model, i says update successfully but the field doesnt even change.

so, what seem to be the problem here. can someone help me

and here the form.py, view.py and model.py

class UpdateProfileForm(ModelForm):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for visible in self.visible_fields():
            visible.field.widget.attrs['class'] = 'profile-input disabled'
            # visible.field.widget.attrs['disabled'] = True

    class Meta:
        model = Customer
        fields = ["first_name", "last_name",  "email", "phone_number"]

view.py

@login_required(login_url="login")
def profile_page(request, pk):

    if request.method == "POST":
        update_profile_form = UpdateProfileForm(request.POST, instance=request.user)

        if update_profile_form.is_valid():
            update_profile_form.save()

            messages.success(request, 'Message: Profile Updated')
            return redirect('profile_page', request.user.username)


    context = {
        'profile_form': UpdateProfileForm(instance=request.user)
    }

    return render(request, 'base/profile_page.html', context)

and model.py

from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db import models
from django.db.models import Sum


class User(AbstractUser):

    class UserTypes(models.TextChoices):
        ADMIN = "ADMIN", "Admin"
        MANAGER = "MANAGER", "Manager"
        CUSTOMER = "CUSTOMER", "Customer"

    BASE_ROLE = UserTypes.ADMIN

    name = models.CharField(max_length=255)
    email = models.EmailField(unique=True)
    phone_number = models.CharField(max_length=24)
    role = models.CharField(max_length=50, choices=UserTypes.choices)

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ["username"]

    def save(self, *args, **kwargs):
        if not self.pk:
            self.role = self.BASE_ROLE
            return super().save(*args, **kwargs)


## Manager Proxy Model and ManagerUserManager
class ManagerUserManager(BaseUserManager):
    def get_queryset(self, *args, **kwargs):
        results = super().get_queryset(*args, **kwargs)
        return results.filter(role=User.UserTypes.MANAGER)


class Manager(User):
    BASE_ROLE = User.UserTypes.MANAGER

    manager = ManagerUserManager()

    class META:
        proxy = True


## Customer Proxy Model and CustomerUserManager
class CustomerUserManager(BaseUserManager):
    def get_queryset(self, *args, **kwargs):
        results = super().get_queryset(*args, **kwargs)
        return results.filter(role=User.UserTypes.CUSTOMER)


class Customer(User):
    BASE_ROLE = User.UserTypes.CUSTOMER

    customer = CustomerUserManager()

    class META:
        proxy = True

Post your code please in code blocks

i wrote the code , i hope u can help. sorry if it little messy

Please try again. Put three backticks ``` on a line on their own at the start and end of each block of code.

You are showing an admin page image here. Are you saying that if you update User in the admin that the fields don’t change? Or are you saying that when your form and view are being used, then the fields aren’t changed?

You’re only calling super() in your save method if that object doesn’t already have a pk (not currently in the database). This is going to prevent you from updating that object when it already exists.

1 Like

it’s kiinda both, when the user (customer) try to update profile using the form and the view, as well with the superuser which i try from the admin page. the changes wasnt apply. says like default fields from django user model (first_name, last_name, is_staff, is_active) everytime i try to update it, the changes not apply

ok thank u for the info, cause i just using the system

so, i assume i have to calling another super().save() outside the conditional statement to handle if the record alredy in database. that kinda make sense to me.

it works, oh that explained. thank u for the solution, i didnt relize that before. cause i just learned about proxy model and overriding model method. thanks again mate, wish u all the best