jacob
jacob

Reputation: 1186

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

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.

Upvotes: 104

Views: 82894

Answers (10)

Scarface97
Scarface97

Reputation: 103

2024 solution (tested in Pycharm Pro):

PyCharm -> Files -> Settings -> Languages & Frameworks -> Django

and check Enable Django Support.

Upvotes: 1

disconnectionist
disconnectionist

Reputation: 508

If you are using PyCharm Community, you can ignore it in your settings: go to "Settings/Editor/Inpections/Unresolved references"

And then in "Options/Ignored references" add this line, replacing the name of your project. This will get rid of this warning. Ignoring this has had no negative effects for me.

<name of your project>.models.*

Upvotes: 0

eriker75
eriker75

Reputation: 61

In pycharm professional you can do that:

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

and then check Enable Django Support

If you are using pycharm community you can do it;

add models.Manager() at your objects model propery

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

additionally you can use pip install django-stubs

Upvotes: 5

sunset
sunset

Reputation: 81

You can try install django-stubs

pip install django-stubs

Upvotes: 8

Campi
Campi

Reputation: 2307

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()

Upvotes: 51

Joseph Bani
Joseph Bani

Reputation: 684

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)

Upvotes: 23

Christopher Davies
Christopher Davies

Reputation: 194

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

Upvotes: 3

winux
winux

Reputation: 482

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

PyCharm Editions Comparison

Upvotes: 3

vishes_shell
vishes_shell

Reputation: 23484

You need to enable Django support. Go to

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

and then check Enable Django Support

Upvotes: 194

Yarh
Yarh

Reputation: 1098

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

Upvotes: 0

Related Questions