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.
6 Answers
You need to enable Django support. Go to
PyCharm -> Preferences -> Languages & Frameworks -> Django
and then check Enable Django Support
-
55
-
2@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. May 9, 2017 at 15:36
-
4@vishes_shell if i just disable this inspection, the auto complete will be also disable. is there some other ways to solve this problem? May 15, 2017 at 10:49
-
7@vishes_shell The question was about the Community Edition. I believe the correct answer is that it can not be done.– kraxorNov 16, 2017 at 16:14
-
1In version 4.5 of PyCharm is Django/objects supported in the community version.– JonMar 8, 2018 at 20:12
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?– VishalFeb 21, 2018 at 23:59 -
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.– CampiFeb 22, 2018 at 7:39
-
I think the correct syntax would be
from django.db import models
now. Nov 3, 2020 at 9:06 -
1
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)
-
1
-
No, because the class is basically the same, you better make the BaseModel abstract though. updating my answer Oct 20, 2019 at 5:02
Python Frameworks (Django, Flask, etc.) are only supported in the Professional Edition. Check the link below for more details.
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:
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.– JannJan 27, 2020 at 16:40
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