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