Pycharm can't auto complete some modules

人盡茶涼 提交于 2020-01-16 09:06:24

问题


Pycharm works well at most time. But it can't auto complete some code.

The following "objects" can't be completed. Who knows why?

I use Pycharm Community Edition and Django 2.2

class SnippetList(APIView):
    """
    List all snippets, or create a new snippet.
    """

    def get(self, request):
        snippets = Snippet.objects.all()  # The objects can't be auto completed.
        serializer = SnippetSerializer(snippets, many=True)
        return Response(resp)

Power Save Mode is off.


回答1:


Sometimes this happend if you have enabled de Power Save mode. Check the icon of a hat at the lower right corner.




回答2:


Django is part of the Professional Edition so you need it for completion to work. See this




回答3:


It seems like you are using Django.
PyCharm Community edition does not offer autocompletion on Django (I haven't used the professional version).
To use the PyCharm autocomplete, you have to use the type hinting feature of python, available from python3.5 with some other features added on python3.6.
You can use django-hint module which helps you in type hinting.
For example, if you write your model this way:

from django.db import models
from django_hint import StandardModelType

class Snippet(models.Model, StandardModelType):
    """Just like any other model"""
    pass

inherited from StandardModelType, while making a query, objects will be offered as an autocomplete. StandardModelType does not affect your database and does not create a migration.
Note that you need python3.6 or higher to use django-hint

Disclaimer: I am the author of django-hint




回答4:


I found a solution, it works at the objects level, deep level still not work.

# pip install django-stubs

class BaseModel(models.Model):
    objects = models.Manager()

    class Meta:
        abstract = True

class Snippet(BaseModel):

    created = models.DateTimeField(auto_now_add=True)


来源:https://stackoverflow.com/questions/58625742/pycharm-cant-auto-complete-some-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!