0

I have a simple django app. Inside the models.py folder i have:

class TestModel(models.Model):
    name = models.CharField(max_length=300)

    def get_all(self):
        all = TestModel.objects.all()
        return all

Even though I have instances of TestModel in my database (PostgreSQL), the function get_all returns None. PyCharm helped me in narrowing down the problem by marking objects and telling me that it Cannot find a declaration to go to. If I create the project with PyCharm Professional through the UI, this problem does not occur. As soon as I open the application through PyCharm Community, the problem appears and does NOT go away by switching back to Professional.

Django version - 3.2.9 and 4.0.0 tested

Questions:

  1. How to make the Django recognize objects by using PyCharm Community?
  2. What is the cause for this strange behaviour?
Tary
  • 55
  • 7
  • Look at the answers to [this question](https://stackoverflow.com/questions/49749981/pycharm-cannot-find-declaration-to-go-to). – evergreen Apr 22 '22 at 17:40
  • I tried to invalidate caches and to mark the folder with manage.py as sources root, unfortunately neither worked. The function still believes that the Model has no `objects` property and consequently returns `None`, making queries impossible. – Tary Apr 22 '22 at 17:51
  • Try adding `objects = models.Manager()` explicitly to your model. – evergreen Apr 22 '22 at 18:03
  • I did, unfortunately it did not help as the query still returned None with plenty of instances in the DB :\ – Tary Apr 22 '22 at 18:11
  • I have never used PyCharm, but from what I found by searching the Community Edition has some problems with Django. You can try looking at [this reference](https://automationpanda.com/2017/09/14/django-projects-in-pycharm-community-edition/), or maybe find some others, and see if you find anything that helps. – evergreen Apr 22 '22 at 18:23

0 Answers0