Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance of 'OneToOneField' has no 'username' member

Tags:

python

django

I got the following error when I created Profile model

Instance of 'OneToOneField' has no 'username' member

This is the snippet of the code I created

from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default="default.jpg", upload_to="profile_pics")

    def __str__(self):
        return f"{self.user.username} Profile"

previously it was working fine. Now, all of a sudden I am getting this error.I didn't understand the meaning of this error. How do I solve it? Thank you

like image 992
Asif Avatar asked Mar 17 '19 07:03

Asif


1 Answers

In order for pylint to work properly with Django you should install the pylint-django packege:

pip install pylint-django

Then you can run pylint with pylint_django as a plugin:

pylint --load-plugins pylint_django <path_to_django_file>

If you are using VSCode as your IDE, you can add this snippet to your .vscode/settings.json file to load the plugin for your project:

{
    "python.linting.pylintArgs": [
        "--load-plugins",
        "pylint_django"
    ]
}

Or, if you have a .pylintrc file, you can add this line to load the plugin:

[MASTER]
load-plugins=pylint_django

You can find out more on about pylint-django here.

like image 101
Pumi Avatar answered Oct 22 '22 15:10

Pumi