Pylint warning - Class 'User' has no 'objects' member (no-member)
PyLint is complaining:
Class 'User' has no 'objects' member (no-member)Class 'User' has no 'DoesNotExist' member (no-member)
This is because objects is assigned in the metaclass base/metaclasses.py:345:
# Provide a default queryset unless exists or one has been set
if 'objects' not in dir(new_class):
new_class.objects = QuerySetManager()
I think having it defined in the base document would have the effect but be more discoverable:
class Document(BaseDocument):
objects = QuerySetManager()
Similarly you could do
from mongoengine.queryset import DoesNotExist, MultipleObjectsReturned
class Document(BaseDocument):
DoesNotExist = DoesNotExist
MultipleObjectsReturned = MultipleObjectsReturned
Also the per-class exceptions can have their names to include model name:
# Merge in exceptions with parent hierarchy
exceptions_to_merge = (DoesNotExist, MultipleObjectsReturned)
module = attrs.get('__module__')
for exc in exceptions_to_merge:
_name = exc.__name__
parents = tuple(getattr(base, _name) for base in flattened_bases
if hasattr(base, _name)) or (exc,)
# Create new exception and set to new_class
exception = type(name + _name, parents, {'__module__': module})
setattr(new_class, _name, exception)
Thus Sentry having such an exceptions + __repr__ would give hint what model the exception belongs to.
+1
+1
+1
+1
The same issue in Django is solved thanks to a Pylint plugin.
Indeed, in an ideal world, you'd rather make the checker more clever than adapt the software to the checker's shortcomings.
If it is too much work, it might not be worth the pain, though.
Hopefully, we only need the equivalent of a small subpart of the Django checker. From a quick glance, it looks like this file addresses those issues.
+1
@lafrech
Indeed, in an ideal world, you'd rather make the checker more clever than adapt the software to the checker's shortcomings.
I don't thing adapting a checker to a particular project is also a clever idea. For example autocompletion in PyCharm also fails in this case.
pylint is still failing on .objects call - any workaround?
same here
Any update on this issue? It has been almost a year ...
I added "Python for VSCode" add-on and the problem is resolved in my case.
@javsal I added the same extension but it still throws that same error :/
@javsal I installed "Python for VSCode" add-on and still have the error
Class 'somemodel' has no 'objects' member (no-member)
it's some workaround ?
Same issue using "Python for VSCode".
pip install pylint-django
And add to VSC config:
"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],
"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],
it will solve the question, but it let my pylint all failure. whats happening and how to fix it?
"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],
Makes my pylint not working too @ghost, did you managed to make it work?
I was using pylint_django but when I tried to upgrade, it stopped working with mongoengine so I wrote this: https://github.com/jucacrispim/pylint-mongoengine
@JMGama Me too, are you fix this ?
from django.shortcuts import render
from .models import Job
def home(request):
jobs = Job.objects
return render(request, 'jobs/home.html', {"jobs":jobs} )`
I am getting error
{
"resource": "/c:/Users/Akhil/Desktop/django_stuff/portfolio-project/jobs/views.py",
"owner": "python",
"code": "no-member",
"severity": 8,
"message": "Class 'Job' has no 'objects' member",
"source": "pylint",
"startLineNumber": 8,
"startColumn": 12,
"endLineNumber": 8,
"endColumn": 12
}
Class 'Job' has no 'objects' member
What should i do now ?
https://github.com/Meet-Kalariya/portfolio-project
setp 1. pip install pylint-django
setp 2. open VSCode -> File -> Preferences -> Settings -> search 'python.linting.pylintArgs'
setp 3. Add this in json.
"python.linting.pylintArgs" :[
"--load-plugins=pylint_django"
]
Edit "C:\Users\anmar\AppData\Roaming\Code\User\settings.json" and add
python.linting.pylintArgs lines at the end as shown below:
{
"team.showWelcomeMessage": false,
"python.dataScience.sendSelectionToInteractiveWindow": true,
"git.enableSmartCommit": true,
"powershell.codeFormatting.useCorrectCasing": true,
"files.autoSave": "onWindowChange",
"python.linting.pylintArgs": [
"--load-plugins=pylint_django",
"--errors-only"
],
}
Edit "C:\Users\anmar\AppData\Roaming\Code\User\settings.json" and add python.linting.pylintArgs lines at the end as shown below: { "team.showWelcomeMessage": false, "python.dataScience.sendSelectionToInteractiveWindow": true, "git.enableSmartCommit": true, "powershell.codeFormatting.useCorrectCasing": true, "files.autoSave": "onWindowChange", "python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--errors-only" ], }
Great, this worked for me
def post_list(request): posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') return render(request, 'blog/post_list.html', {'posts': posts })
I had an issue where Post wouldn't let me use "objects" as it wasn't a member of the "Post" import, the setting this person posted helped me a lot, thanks!
if we do this only `"python.linting.pylintArgs": [ "--load-plugins=pylint_django"
],`
it gives other errors but this does magic
"python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--errors-only" ],
thanks for help
I fixed this issue by:
First, install python package pylint-django
pip install pylint-django
then open the project folder .vscode/settings.json and add:
{
...
"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],
...
}
then the message was gone.
def home(request): # pylint: disable=no-member <--------This is just a workaround to get rid of warnings jobs = Job.objects return render(request, 'jobs/home.html', {"jobs":jobs} )`
I got the same error when implementing Model.get_FOO_display() - Django; in vscode .
IMPLEMENTATION.
from django.db import models
class Plan(models.Model):
PLAN_CHOICES = [
('T', 'Talk More'),
('S', 'STANDARD),
]
name = models.CharField(
choices=PLAN_CHOICES,
default='S',
max_length=255,
)
def __str__(self):
return self.get_name_display()
ERROR
return self.get_name_display()
Instance of 'Plan' has no 'get_name_display' memberpylint(no-member)
WORK FORM ME
join @david-daming and @ajaysbugatti Thanks!!!
ENGINES
- Python==3.8.2
- Django==3.0.8
- pylint==2.5.3
- pylint-django==2.1.0
- pylint-plugin-utils==0.6
I did the: "python.linting.pylintArgs": [ "--load-plugins=pylint_django" ], and "python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--errors-only" ], and stop the problems messages, but also stop other error messages
use pylint --disable no-member to suppress no-member error messages.
For me it is working..
Similar need here: #2326