232
def index(request):
   latest_question_list = Question.objects.all().order_by('-pub_date')[:5]
   template = loader.get_template('polls/index.html')
   context = {'latest_question_list':latest_question_list}
   return HttpResponse(template.render(context, request))

The first line of that function gets an error on Question.objects.all():

E1101: Class 'Question' has no 'objects' member

I'm following the Django documentation tutorial and they have the same code up and running.

I have tried calling an instance.

Question = new Question()
and using MyModel.objects.all()

Also my models.py code for that class is this...

class Question(models.Model):
    question_text = models.CharField(max_length = 200)
    pub_date = models.DateTimeField('date published') 

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

    def __str__(self):
        return self.question_text

To no avail I still have this error.

I have read about pylint and ran this...

pylint --load-plugins pylint_django

Which didn't help, even tho the github readme file says...

Prevents warnings about Django-generated attributes such as Model.objects or Views.request.

I ran the command within my virtualenv, and yet nothing.

So any help would be great.

kriss
  • 23,497
  • 17
  • 97
  • 116
buuencrypted
  • 2,643
  • 2
  • 10
  • 8

18 Answers18

434

Install pylint-django using pip as follows

pip install pylint-django

Then in Visual Studio Code goto: User Settings (Ctrl + , or File > Preferences > Settings if available ) Put in the following (please note the curly braces which are required for custom user settings in VSC):

{"python.linting.pylintArgs": [
     "--load-plugins=pylint_django"
],}
Community
  • 1
  • 1
tieuminh2510
  • 4,449
  • 1
  • 11
  • 6
  • 1
    where is User Settings – cegprakash May 23 '18 at 13:46
  • Go to File->Preferences->Settings or use "Ctrl + Comma" – Sukma Saputra May 31 '18 at 01:02
  • 48
    Excellent! I'd also point out that you can put the plugin option in a `.pylintrc` file like so: `load-plugins=pylint_django` And that way it will be picked up by the CLI as well, so it would work in a continuous integration setting. – sibnerian Jul 07 '18 at 17:36
  • 2
    After doing so i am getting `[pylint] C0111:Missing module docstring` error – Vishnu Sharma Jul 16 '18 at 06:30
  • @VishnuSharma, add a comment after or before the module – Lutaaya Huzaifah Idris Aug 25 '18 at 20:29
  • I am getting new errors after applying this solution. Switching to the flask8 linter as suggested by another solution worked better for me. – Erik Kalkoken Jun 21 '19 at 15:23
  • 1
    More details in the official doc: https://code.visualstudio.com/docs/python/linting#_commandline-arguments-and-configuration-files – Anton Danilchenko Sep 26 '19 at 11:55
  • I would suggest to check the link provided by @AntonDanilchenko, because the way the parameters are specified has changed for VS Code 1.40.0 and they way provided by this answer doesn't work anymore (as I just have tested). They way described by the link works. – amedina Nov 09 '19 at 18:42
  • How should PyCharm (Community Edition) users proceed to have pylint-django to work ? – AniMir Feb 11 '20 at 11:23
  • does not seem to work with django 2.2 and python 3.6. the pylint_django does not seem to detect reverse OneToOne and I still get E1101 for those references. – Xerion Feb 11 '20 at 16:12
  • add `{"python.linting.pylintArgs": ["--load-plugins=pylint_django"],}` to .vscode/settings.json after `pip install pylint-django` – Afsan Abdulali Gujarati Apr 09 '20 at 02:07
  • my mac complains when I use pip directly and I have python3 installed so for the first step I did `python3 -m pip install pylint-django`. – Adam Labi Apr 28 '20 at 18:35
  • 2
    @VishnuSharma by adding the configuration. **it disables the default values of Pylint**, so you should add default values manually. You can do so as followed: `{"python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--disable=all", "--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode" ]}` [for more info](https://code.visualstudio.com/docs/python/linting#_commandline-arguments-and-configuration-files) – Oded .S Oct 06 '20 at 17:43
  • This doesn't work in the new version of VSC. Please see the answer below. – Yuri.teacher.English Mar 13 '21 at 16:00
168

@tieuminh2510's answer is perfect. But in newer versions of VSC you will not find the option to edit or paste that command in User Settings.

For newer versions, add the code in the following steps:

  1. Press ctrl shift p to open the the Command Palette.
  2. Now in Command Palette type Preferences: Configure Language Specific Settings.
  3. Select Python.
  4. Add these lines inside the first curly braces:
    "python.linting.pylintArgs": [
            "--load-plugins=pylint_django",
        ]

Make sure that pylint-django is also installed.

user5305519
  • 3,008
  • 4
  • 26
  • 44
FightWithCode
  • 2,190
  • 1
  • 13
  • 24
  • 10
    don't forget to put a comma in the json file otherwise, it will not work correctly. – Georgi Stoyanov Feb 05 '19 at 13:52
  • 5
    Also don't forget to `pip install pylint-django`. If the extension is not installed, this change to your settings seems to fail quietly, breaking all linting. – Dustin Michels Apr 02 '19 at 23:49
  • 2
    Thank you. You can find the same info in the official VS Code documentation here: https://code.visualstudio.com/docs/python/linting#_commandline-arguments-and-configuration-files – Anton Danilchenko Sep 26 '19 at 11:46
  • 3
    It solved the earlier error. But, now a warning is coming. ```Missing module docstringpylint(missing-module-docstring)```. What can be done for this ? – Optider Jul 03 '20 at 08:30
  • This also has to be added "--django-settings-module=trydjango.settings" – pcsaunak Jan 30 '21 at 06:47
48

Install Django pylint:

pip install pylint-django

ctrl+shift+p > Preferences: Configure Language Specific Settings > Python

The settings.json available for python language should look like the below:

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

    "[python]": {

    }
}
user3785966
  • 2,578
  • 26
  • 18
41

I've tried all possible solutions offered but unluckly my vscode settings won't changed its linter path. So, I tride to explore vscode settings in settings > User Settings > python. Find Linting: Pylint Path and change it to "pylint_django". Don't forget to change the linter to "pylint_django" at settings > User Settings > python configuration from "pyLint" to "pylint_django".

Linter Path Edit

Willy satrio nugroho
  • 908
  • 1
  • 16
  • 27
32

Heres the answer. Gotten from my reddit post... https://www.reddit.com/r/django/comments/6nq0bq/class_question_has_no_objects_member/

That's not an error, it's just a warning from VSC. Django adds that property dynamically to all model classes (it uses a lot of magic under the hood), so the IDE doesn't know about it by looking at the class declaration, so it warns you about a possible error (it's not). objects is in fact a Manager instance that helps with querying the DB. If you really want to get rid of that warning you could go to all your models and add objects = models.Manager() Now, VSC will see the objects declared and will not complain about it again.

buuencrypted
  • 2,643
  • 2
  • 10
  • 8
23

UPDATE FOR VS CODE 1.40.0

After doing:

$ pip install pylint-django

Follow this link: https://code.visualstudio.com/docs/python/linting#_default-pylint-rules

Notice that the way to make pylint have into account pylint-django is by specifying:

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

in the settings.json of VS Code.

But after that, you will notice a lot of new linting errors. Then, read what it said here:

These arguments are passed whenever the python.linting.pylintUseMinimalCheckers is set to true (the default). If you specify a value in pylintArgs or use a Pylint configuration file (see the next section), then pylintUseMinimalCheckers is implicitly set to false.

What I have done is creating a .pylintrc file as described in the link, and then, configured the following parameters inside the file (leaving the rest of the file untouched):

load-plugins=pylint_django

disable=all

enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode

Now pylint works as expected.

amedina
  • 2,838
  • 3
  • 19
  • 40
  • 3
    Instead of creating a separate .pylintrc file, you can get the same effect by doing this in your settings.json: { "python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--disable=all", "--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode" ] } – sql_knievel May 02 '20 at 15:45
14

You can change the linter for Python extension for Visual Studio Code.

In VS open the Command Palette Ctrl+Shift+P and type in one of the following commands:

Python: Select Linter

when you select a linter it will be installed. I tried flake8 and it seems issue resolved for me.

moth
  • 427
  • 1
  • 4
  • 18
11

Just adding on to what @Mallory-Erik said: You can place objects = models.Manager() it in the modals:

class Question(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    # ...
    def __str__(self):
        return self.question_text
    question_text = models.CharField(max_length = 200)
    pub_date = models.DateTimeField('date published')
    objects = models.Manager()
LeRoy
  • 4,189
  • 2
  • 35
  • 46
7

I was able to update the user settings.json

On my mac it was stored in:

~/Library/Application Support/Code/User/settings.json

Within it, I set the following:

{
    "python.linting.pycodestyleEnabled": true,
    "python.linting.pylintEnabled": true,
    "python.linting.pylintPath": "pylint",
    "python.linting.pylintArgs": ["--load-plugins", "pylint_django"]
}

That solved the issue for me.

Kingsley Ijomah
  • 3,273
  • 33
  • 25
6

Change your linter to - flake8 and problem will go away.

Venu Gopal Tewari
  • 5,672
  • 42
  • 41
  • 1
    I found this useful, clicked CMD + SHIFT + P and searched Python: Select Linter and just select flake8! – Ayyoub Aug 12 '20 at 12:22
4

First install pylint-django using following command

$ pip install pylint-django

Then run the second command as follows:

$ pylint test_file.py --load-plugins pylint_django

--load-plugins pylint_django is necessary for correctly review a code of django

Chirag Kalal
  • 638
  • 1
  • 7
  • 21
3

If you use python 3

python3 -m pip install pylint-django

If python < 3

python -m pip install pylint-django==0.11.1

NOTE: Version 2.0, requires pylint >= 2.0 which doesn’t support Python 2 anymore! (https://pypi.org/project/pylint-django/)

Alberto
  • 103
  • 3
Ashen One
  • 371
  • 1
  • 12
3

First, Install pylint-django using pip as follows:

pip install pylint-django

Goto settings.json find and make sure python linting enabled is true like this:

enter image description here

At the bottom write "python.linting.pylintPath": "pylint_django"like this:

enter image description here

OR,

Go to Settings and search for python linting

make sure Python > Linting: Pylint Enabled is checked

Under that Python > Linting: Pylint Path write pylint_django

Mahbub Ul Islam
  • 773
  • 8
  • 15
  • 1
    thank you sir, this was driving me crazy, because i was trying everything that all the other answers were suggesting, but nothing worked for me until i found your answer and read that i needed to add the pylint django path to settings.json "python.linting.pylintPath": "pylint_django", – luckyguy73 Aug 30 '21 at 00:41
2

How about suppressing errors on each line specific to each error?

Something like this: https://pylint.readthedocs.io/en/latest/user_guide/message-control.html

Error: [pylint] Class 'class_name' has no 'member_name' member It can be suppressed on that line by:

  # pylint: disable=no-member
  • 3
    As brute-force as this solution is, it's the only way to preserve one's sanity. I'm getting those errors on dozens of different modules, I'm not going to change settings every time I see a new one. – Przemek D May 22 '19 at 10:49
2

I installed PyLint but I was having the error Missing module docstringpylint(missing-module-docstring). So I found this answer with this config for pylint :

"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
    "--disable=C0111", // missing docstring
    "--load-plugins=pylint_django,pylint_celery",
 ],

And now it works

lucrp
  • 582
  • 9
  • 11
  • Are you sure this answers the questions being asked? – anurag Jan 15 '21 at 14:51
  • I tried to install pylint as the first answer `pip install pylint-django` but after that I was having the `Missing module docstringpylint(missing-module-docstring)`. So, I searched and found this [link](https://stackoverflow.com/questions/53304116/enabling-pylint-django-plugin-in-vscode-pylint-stop-working/54881400#54881400) with a configuration for pylint that works for me. So, I think this complements the answers for this question. If I'm doing it wrong, excuse me, but I'm a newbie here. – lucrp Jan 19 '21 at 09:35
  • 1
    I improved my answer. If it is not better, please point it out. – lucrp Jan 19 '21 at 09:57
1

By doing Question = new Question() (I assume the new is a typo) you are overwriting the Question model with an intance of Question. Like Sayse said in the comments: don't use the same name for your variable as the name of the model. So change it to something like my_question = Question().

voodoo-burger
  • 2,123
  • 3
  • 22
  • 29
  • its something with my pc config of django and pytho, i recoded it on my macbook and it works just fine. Also i tried ur suggestion in my code and it made no difference. – buuencrypted Jul 17 '17 at 16:57
0

This issue happend when I use pylint_runner

So what I do is open .pylintrc file and add this

# List of members which are set dynamically and missed by pylint inference
# system, and so shouldn't trigger E1101 when accessed. Python regular
# expressions are accepted.
generated-members=objects
Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
0

Just add objects = None in your Questions table. That solved the error for me.

Pratham Dave
  • 93
  • 1
  • 11