MongoEngine / mongoengine

A Python Object-Document-Mapper for working with MongoDB
http://mongoengine.org
MIT License
4.23k stars 1.23k forks source link

Pylint warning - Class 'User' has no 'objects' member (no-member) #858

Open warvariuc opened 9 years ago

warvariuc commented 9 years ago

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.

jontrainor commented 9 years ago

+1

citizen-stig commented 8 years ago

+1

goldan commented 8 years ago

+1

reallistic commented 8 years ago

+1

lafrech commented 8 years ago

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.

DavidHwu commented 8 years ago

+1

mindojo-victor commented 8 years ago

@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.

boazin commented 8 years ago

pylint is still failing on .objects call - any workaround?

kiddten commented 8 years ago

same here

nguyenkims commented 7 years ago

Any update on this issue? It has been almost a year ...

javsal commented 7 years ago

I added "Python for VSCode" add-on and the problem is resolved in my case.

Hankrecords commented 6 years ago

@javsal I added the same extension but it still throws that same error :/

pabloazurduy commented 6 years ago

@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 ?

gutomarzagao commented 6 years ago

Same issue using "Python for VSCode".

tfalcao commented 6 years ago

pip install pylint-django

And add to VSC config:

"python.linting.pylintArgs": [
    "--load-plugins=pylint_django"
],
ghost commented 6 years ago
"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?

JMGama commented 6 years ago
"python.linting.pylintArgs": [
    "--load-plugins=pylint_django"
],

Makes my pylint not working too @ghost, did you managed to make it work?

jucacrispim commented 6 years ago

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

geknow commented 5 years ago

@JMGama Me too, are you fix this ?

meetkalariya commented 5 years ago
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

Daviswww commented 5 years ago

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"
]

nemodev76 commented 5 years ago
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"
    ],
}
Cristroyer commented 4 years ago
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!

ajaysbugatti commented 4 years ago

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

david-daming commented 4 years ago

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.

swapnildongre89 commented 4 years ago

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} )`
cesardmn commented 4 years ago

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

ghost commented 4 years ago

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

npscode commented 3 years ago

use pylint --disable no-member to suppress no-member error messages.

For me it is working..

NicolasM-Forsk commented 1 year ago

Similar need here: #2326

glebignatieff commented 1 year ago

That's a really annoying issue. Why would one install pylint_django even if they don't use Django at all?

xday2000 commented 1 year ago

use pylint --disable no-member to suppress no-member error messages.

For me it is working..

i followed this and it works but... i just don't know if this is right... doesn't this issue have a perfect solution?