Ensure Customuser is being used in Django App
I have a problem with implementing CustomUser in a Django App.
My project is called "VendorManagement".
My app is "VMP".
In settings, I have AUTH_USER_MODEL = 'VMP.CustomUser'
set.
Here is my CustomUser Model:
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User, AbstractUser
from django.utils.timezone import timedelta, now
from django.core.exceptions import ValidationError
# File validation function
def validate_file_type(value):
allowed_types = ["application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
if value.content_type not in allowed_types:
raise ValidationError("Only PDF and Word documents are allowed.")
class CustomUser(AbstractUser):
ROLE_CHOICES = [
('vendor', 'Vendor'),
('customer', 'Customer'),
('admin', 'Admin'),
]
role = models.CharField(max_length=20, choices=ROLE_CHOICES, default='customer')
groups = models.ManyToManyField("auth.Group", related_name="custom_user_groups", blank=True)
user_permissions = models.ManyToManyField("auth.Permission", related_name="custom_user_permissions", blank=True)
def is_vendor(self):
return self.role == "vendor"
def is_customer(self):
return self.role == "customer"
def is_admin(self):
return self. Role == "admin"
Here is my views:
from django.contrib.auth import get_user_model
from .models import *
# Get custom User model
CustomUser = get_user_model()
class RFPViewSet(viewsets.ModelViewSet):
serializer_class = RFPSerializer
def get_queryset(self):
"""Vendors see active RFPs, Customers see expired ones"""
userid = self.request.user.id
# Ensure user is fetched correctly as CustomUser
user_instance = CustomUser.objects.filter(id=user.id).first()
if user_instance.is_vendor():
return RFP.objects.filter(is_expired=False, is_active=True)
elif user_instance.is_customer():
return RFP.objects.filter(customer=user_instance, is_expired=True)
return RFP.objects.none()
The problme3 i that while the model does include is_customer()
and is_vendor()
, the functions are NOT found in the ViewSet code.
it says that the functions are not visible. In my IDE, it says that my type of variable is "AbstractUser" and not "CustomUser".
how do I fix this? THanks
I can see two issues with your code, one is the CustomUser model and one in the ViewSet.
In the custom user model's is_admin
method, you wrote:
self. Role
where it should be self.role
.
In the ViewSet, you defined the userid
variable but then use the user.id to get the CustomUser instance.
Instead write it like this:
class RFPViewSet(viewsets.ModelViewSet):
serializer_class = RFPSerializer
def get_queryset(self):
"""Vendors see active RFPs, Customers see expired ones"""
user = self.request.user
# No need to fetch as user is already a Custom User
if user.is_vendor():
return RFP.objects.filter(is_expired=False, is_active=True)
elif user.is_customer():
return RFP.objects.filter(customer=user_instance, is_expired=True)
return RFP.objects.none()
Hope this helps! Do let me know if this solves it.
Thanks!
First of all, there is typo in your model:
def is_admin(self):
return self. Role == "admin"
Secondly, you are extending AbstractUser
which is good when you want few tweaks above django's default User
. In case of more customization, it's better to use AbstractBaseUser
. AbstractBaseUser
is the superclass of AbstractUser
. You can find about configuring it here.