[Python] Django OneToOneField, Pyright: Cannot access attribute (reportAttributeAccessIssue)

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

  1. Stack

    Stack Membro Participativo

    I try to check my Django project with pyright.

    There is this OneToOneField, which pyright does not detect, when I use it:

    user.lala


    Error message of pyright:


    error: Cannot access attribute "lala" for class "User" Attribute "lala" is unknown (reportAttributeAccessIssue)
    ​

    # file lala/models.py

    class LaLaUser(models.Model):
    user = models.OneToOneField(
    User, on_delete=models.CASCADE, primary_key=True, related_name="lala"
    )
    discount = models.PositiveSmallIntegerField(
    default=0,
    verbose_name="Discount (0 bis 100)",
    validators=[MinValueValidator(0), MaxValueValidator(100)],
    )

    def __str__(self):
    return self.user.username


    The django-stubs are installed, and all other Django magic works fine with pyright.

    Version: Django 5.2, pyright 1.1.404.

    How to make pyright understand that OneToOneField?

    (ignoring that via a comment is not an answer)

    Continue reading...

Compartilhe esta Página