I am following the Django official tutorial and getting stuck by the following error:
Class ‘Question’ has no ‘objects’ member pylint(no-member)
I have exact the same code as used in the tutorial but still having the error. I found on StackOverflow this issue (https://stackoverflow.com/questions/45135263/class-has-no-objects-member) and followed the solution which is provided, unfortunately I getting other errors afterwards --> Missing function or method docstringpylint(missing-function-docstring)
The code in my settings.json is:
"workbench.colorTheme": "Atom One Dark",
The code in my views.py (where the issue occurs) is:
from django.http import HttpResponse
from django.shortcuts import render
from .models import Question
# Create your views here.
latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
def detail(request, question_id):
return HttpResponse("You're looking at question %s" % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
Like to hear!
Where are you getting that message? If it’s just a pylint message, you can ignore it. If it’s a message you’re getting when you are running your program, that’s a different issue.
(Personally, I would ignore it at this time. If you’re just starting to climb the learning curve, I wouldn’t be too worried at the moment about such niceties as pylint.)
pylint is an optional tool and probably too strict for most Python code. It often throws false positives - reporting errors where there are none. Probably here it can’t inspect some of the “magic” that Django does to create the “objects” attribute.
As Ken says, you can ignore it. If it’s built into your editor, I’d recommend disabling it. I prefer the “flake8” tool - it performs much the same job, but more accurately.
See Luke Plant’s blog post on Pylint false positives.
Here is where the error occurs:
Thanks, I will try to ignore the error, but probably I will be annoyed later on by the error
Do you mean that the Python code doesn’t understand what happens when the models creating the “Question” objects?
pylint is a tool, separate to both Python and Django. It’s made by different people who have certain opinions about how Python code should be structured.
It is trying to detect the
objects attrbiute on the
Question class. You don’t define this attribute in your class, but Django creates it for you (it’s your model manager. Pylint can’t trace the Django code that creates
objects, so it thinks
Question.objects does not exist, and highlights it as a bug. But it’s not.
If you search your editor settings, there will be some way to disable pylint. It seems you’re using vscode - here are the instructions: https://code.visualstudio.com/docs/python/linting#_enable-linters
Thank you for this explanation, bit by bit I am getting to find my way in Django.
It’s a good practice to use pylint as strict as possible. You are doing great!
You can use pylint-django to imporve linting of Django.
It can get rid of this false-positive warning.
If you are using VSCode. Remember to add the configuration in settings: