7

Why am I seeing this warning for a class which is a subclass of models.Model (Foo is defined as class Foo(models.Model))? This is wherever I use Foo.objects.filter(...).

Responding to request for more detail with a simplified example:

# ------ models.py ---------
from django.db import models

class Foo(models.Model):
    pass

# ------ views.py ---------
from models import Foo

inquiry = Foo.objects.filter(...)  # PyCharm gives warning for objects here
    ...

PyCharm gives no warnings for the import statements in either file.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Mitch
  • 2,350
  • 7
  • 29
  • 48

2 Answers2

12

Is your pycharm version community or professional? If your pycharm is community, maybe it needs a pluggin to support django. If your pycharm is professional, make sure that in: Preferences > Languages&Frameworks > Django > Enable Django Support the option is chosen. Here is the image:

dev.doc
  • 569
  • 3
  • 12
  • 18
GanQiang
  • 121
  • 1
  • 4
2

There is a better way to resolve this problem

When you enable the Django support in PyCharm it automatically detects that this is a model and objects refers to the Model manager

Instead you can specify that in your models.py itself, which is the preferred method and the best way to code

update your code like

class Foo(models.Model):
    // column definitions
    objects = models.Manager()