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

PythonDjangoPycharm

Python Problem Overview


I use community pycharm and the version of python is 3.6.1, django is 1.11.1. This warning has no affect on running, but I cannot use the IDE's auto complete.

Python Solutions


Solution 1 - Python

You need to enable Django support. Go to

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

and then check Enable Django Support

Solution 2 - Python

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 - Python

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

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

PyCharm Editions Comparison

Solution 5 - Python

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

Solution 6 - Python

Another solution i found is putting @python_2_unicode_compatible decorator on any model. It also requires you to have a str implementation four your function

For example:

# models.py

from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class SomeModel(models.Model):
    name = Models.CharField(max_length=255)

    def __str__(self):
         return self.name

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionzhiang.shiView Question on Stackoverflow
Solution 1 - Pythonvishes_shellView Answer on Stackoverflow
Solution 2 - PythonCampiView Answer on Stackoverflow
Solution 3 - PythonJoseph BaniView Answer on Stackoverflow
Solution 4 - PythonwinuxView Answer on Stackoverflow
Solution 5 - PythonChristopher DaviesView Answer on Stackoverflow
Solution 6 - PythonYarhView Answer on Stackoverflow