Unresolved attribute reference 'objects' for class '' in PyCharm

Solution 1:

You need to enable Django support. Go to

PyCharm -> Preferences -> Languages & Frameworks -> Django

and then check Enable Django Support

Solution 2:

You can also expose the default model manager explicitly:

from django.db import models

class Foo(models.Model):
    name = models.CharField(max_length=50, primary_key=True)

    objects = models.Manager()

Solution 3:

Use a Base model for all your models which exposes objects:

class BaseModel(models.Model):
    objects = models.Manager()
    class Meta:
        abstract = True


class Model1(BaseModel):
    id = models.AutoField(primary_key=True)

class Model2(BaseModel):
    id = models.AutoField(primary_key=True)

Solution 4:

Python Frameworks (Django, Flask, etc.) are only supported in the Professional Edition. Check the link below for more details.

PyCharm Editions Comparison

Solution 5:

I found this hacky workaround using stub files:

models.py

from django.db import models


class Model(models.Model):
    class Meta:
        abstract = True

class SomeModel(Model):
    pass

models.pyi

from django.db import models

class Model:
    objects: models.Manager()

This should enable PyCharm's code completion: enter image description here

This is similar to Campi's solution, but avoids the need to redeclare the default value