[Python] ensure Customuser is being used in Django App

Discussão em 'Python' iniciado por Stack, Abril 30, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página