Pylint giving false errors on Django project

Question:

I’m using VSCode and the Pylint that ships with it, i.e., no extension.
Everything has been running smooth for many months and I’ve never had an issue with Pylint presenting weird alerts.

I recently started learning Django and today when following the official Django Documentation tutorial part 4 pylint could not recognize a couple of statements related to a model.

selected_choice = question.choice_set.get(pk=request.POST['choice'])
return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) 

The errors I get from pylint are below

choice_set: Unknown
Cannot access member "choice_set" for type "Question"
  Member "choice_set" is unknownPylancereportGeneralTypeIssues

and

id: Unknown
Cannot access member "id" for type "Question"
  Member "id" is unknownPylancereportGeneralTypeIssues

At first I thought the issue might’ve been I’d forgot to cross reference the Question and Choice models, but this is not the case. For reference, here are the related files for the project

I can run the project without errors.

To momentarily solve this problem I just told pylint to ignore this, but I don’t fell this best practice. Maybe I’m missing something here?

below the code I ended up writing for these two lines. Any hints?

#(...)
selected_choice = question.choice_set.get(pk=request.POST['choice']) #type: ignore
#(...)
return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) #type: ignore

Solution in trial:

  1. pylint-django
  • installed via pip (on the virtual environment)
  • added parameters to the settings.json file as per below
    settings.json

nothing changed.

Asked By: Falconius

||

Answers:

As mentioned by Pierre, pylint-django addresses these issues.

If you’re using vs-code you’ll not find an extension on the marketplace. This means you will need to install pylint via pip. Vs-code will not recognize the package, so you will need to add some configurations and fiddle with your workspace or user’s settings.json

The article
PyLint for Django in VSCode helped too

Answered By: Falconius

You can use command pip install pylint-django to install pylint-django

and add the following codes to your settings.json:

"python.linting.pylintArgs": [
    "--load-plugins=pylint_django"
]
Answered By: MingJie-MSFT