简体   繁体   中英

Pycharm can't auto complete some modules

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.

在此处输入图像描述

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

在此处输入图像描述

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM