You need to enable Django support. Go to
PyCharm -> Preferences -> Languages & Frameworks -> Django
and then check Enable Django Support
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()
models.Manager()
and it has the added benefit of not breaking PyCharm CE. Is exposing the default model manager un-pythonic?
from django.db import models
now.
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)
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()
https://i.stack.imgur.com/8YoXZ.png
This is similar to Campi's solution, but avoids the need to redeclare the default value
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
Success story sharing