ChatGPT解决这个技术问题 Extra ChatGPT

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.


z
zx485

You need to enable Django support. Go to

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

and then check Enable Django Support


Django is only currently supported in the paid version of pycharm
@EricBlum yep, i know, but PyCharm is awesome. And there is a way to disable such inspections. There is also a way to use different IDEs, such as Atom, Visual Code and others.
@vishes_shell if i just disable this inspection, the auto complete will be also disable. is there some other ways to solve this problem?
@vishes_shell The question was about the Community Edition. I believe the correct answer is that it can not be done.
In version 4.5 of PyCharm is Django/objects supported in the community version.
C
Campi

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

I do this because I have a custom models.Manager() and it has the added benefit of not breaking PyCharm CE. Is exposing the default model manager un-pythonic?
You can add multiple managers to your model. When you access Foo.objects you do access the standard manager so it is not incorrect to expose it. Whether it is un-pythonic, I am not sure.
I think the correct syntax would be from django.db import models now.
@FarzadSoltani thanks for flagging, it's fixed now.
J
Joseph Bani

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)

Will this affect migration adding another model?
No, because the class is basically the same, you better make the BaseModel abstract though. updating my answer
w
winux

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

PyCharm Editions Comparison


E
Eric Aya

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

https://i.stack.imgur.com/8YoXZ.png

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


Nice workaround, but better if it is located in a separate file. Like this, PyCharm wants every class specified in the stub, otherwise you'll get the Error "Cannot find reference 'SomeModel' in 'models.pyi' " when importing SomeModel in another file.
Y
Yarh

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