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.
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"
],}
@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:
Preferences: Configure Language Specific Settings.Python. "python.linting.pylintArgs": [
"--load-plugins=pylint_django",
]
Make sure that
pylint-djangois also installed.
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]": {
}
}