Home > OS >  PyCharm doesn't recognize user instance when using custom user model in Django
PyCharm doesn't recognize user instance when using custom user model in Django

Time:08-29

I have a custom user model in my Django project and when I create an instance of request.user in my views PyCharm doesn't seem to recognize the user instance correctly. The available methods/attributes suggested by PyCharm still point to the built-in Django user model but not to my new one.

Is there any way to set this up properly?

Example:

# settings.py

AUTH_USER_MODEL = 'user.UserProfile'
# models.py custom user model

class UserProfile(AbstractBaseUser, PermissionsMixin):

    # Email and name of the user
    email = models.EmailField(max_length=255, unique=True)
    name = models.CharField(max_length=255)

    # Privilege and security booleans
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)
    email_confirmed = models.BooleanField(default=False)

    # Company on whose behalf the user acts on
    company = models.ForeignKey('company.Company', on_delete=models.CASCADE, blank=True, null=True)

    objects = UserProfileManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def email_user(self, subject, message, from_email=None, **kwargs):
        """Send mail to user - Copied from original class"""
        send_mail(subject, message, from_email, [self.email], **kwargs)

    def __str__(self):
        return self.email
# views.py

def render_dashboard_benefits(request):

    # Get current user instance
    current_user = request.user


    # Typing...
    current_user.company

    # Pycharm suggests 'first_name' and 'last_name' depending on the initial user model
    # but not e.g. "email" or "company" according to the new user model

    return render(request, 'test.html')

Re dudulus answer, this indeed works but raises:

enter image description here

current_user: UserProfile = request.user

so still I think this is an IDE bug?

CodePudding user response:

You can use like this.

current_user: UserProfile = request.user
  • Related