The class has no objects.

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 this function receives an error in Question.objects.all () -> E1101: Class' Question has no objects member`

Im following the Django documentation tutorial and they have the same code that works and works.

I tried to call an instance.

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

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

 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 read about pylint and ran this ...

 pylint --load-plugins pylint_django 

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

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

I executed a command in my virtual space, but nothing.

So any help would be great

+103
source share
12 answers

Install pylint-django using pip as follows

 pip install pylint-django 

Then in the Visual Studio code, go to: User Preferences ( Ctrl + , or File> Preferences> Preferences, if available). Enter the following (note the curly braces required for user settings in VSC):

 {"python.linting.pylintArgs": [ "--load-plugins=pylint_django" ],} 
+245
source

@ tieuminh2510 the answer is perfect. But in newer versions of VSC, you will not find the option to edit or paste this command in the user settings . Now in the new version, to add this code, follow these steps :

Press ctr + sft + P to open the command palette . Now in the command palette, enter Preferences: Configure language-specific settings . Now select Python . Paste this code on the right side

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

Inside the first curly braces. Make sure Pilint Django .

Hope this helps!

+50
source

Here's the answer. Got out of my reddit post ... https://www.reddit.com/r/django/comments/6nq0bq/class_question_has_no_objects_member/

This is not a mistake, it is just a warning from VSC. Django adds that the properties are dynamic for all model classes (he uses a lot of magic under the hood), so the IDE does not know about it while looking at the class, so he warns you about a possible error (this is not the case). objects are actually an instance of the dispatcher that helps with querying the database. If you really want to get rid of this warning, you can go to all your models and add objects = models.Manager () Now VSC will see the objects declared and will no longer complain about it.

+17
source

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

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

Python: select Linter

when choosing a linter, it will be installed. I tried flake8 and it seems the problem is resolved for me.

+8
source

I tried all possible solutions, but, unfortunately, my vscode settings did not change the path to the linter. So, I will try to study vscode settings in settings> User Settings> python . Find Linting: Pylint Path and change it to "pylint_django". Remember to change the linter to "pylint_django" in the settings section > User Preferences> configure python from "pyLint" to "pylint_django".

Linter path edit

+7
source

Install pylint-django first using the 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 required to view django code correctly

+3
source

By doing Question = new Question() (I assume that new is a typo), you rewrite the question model with intance Question . As Sayse said in the comments: do not use the same name for your variable as the model name. So change it to something like my_question = Question() .

+1
source

How about suppressing errors in each row specific to each error?

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

Error: [pylint] The class 'class_name' does not have a member 'member_name'. In this line it can be suppressed as follows:

  # pylint: disable=no-member 
+1
source

Change the linter to - flake8 and the problem will disappear.

+1
source

Install Django Pylint:

 pip install pylint-django 

ctrl + shift + p> Preferences: language setting> Python

The settings.json file available for the Python language should look like this:

 { "python.linting.pylintArgs": [ "--load-plugins=pylint_django" ], "[python]": { } } 
+1
source

Just adding to what @ Mallory-Erik said: You can put objects = models.Manager() in 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() 
0
source

If you are using Python 3

 python3 -m pip install pylint-django 

If python & lt; 3

 python -m pip install pylint-django==0.11.1 

NOTE. Version 2.0, pylint> = 2.0 is required, which no longer supports Python 2! ( https://pypi.org/project/pylint-django/ )

0
source

Source: https://habr.com/ru/post/1269886/


All Articles