Fixing PyCharm error – Unresolved attribute reference ‘objects’ for class ”…”

Unresolved attribute reference ‘objects’ for class ”…” is an error which occurs when you try to use Django with PyCharm which is only supported in the Professional Edition.

In this article I am going to explain what happens when you get this error and how you can solve it with a main solution, we will also explore other solutions which can possibly solve the issue.

Exploring the PyCharm error – Unresolved attribute reference ‘objects’ for class ”…”

This is an error which occurs because Django is not available to use for free with PyCharm.

The error message should look like the message bellow. Make sure you are not dealing with another error.

                                                                       #
Unresolved attribute reference 'objects' for class 'MyClass' 
                                                                       #

Bellow I will present multiple solutions some have worked for me and others have worked for other developers.

Solution 1 : enable Django in Pycharm

If you get this error, you are either trying to use Django and PyCharm without having the professional version of PyCharm or You forgot to enable Django in PyCharm.

The first solution is for those who forgot to enable Django in Pycharm, please jump to the next solution if that is not the case for you.

To enable Django in Pycharm, please start by navigating to Preferences  in PyCharm. Next, go to  Languages & Frameworks and select Django. Voila, the error should be gone.

If that was not the case check out the next solution.

Solution 2 : use a different IDE with PyCharm or expose objects using a Base model for your models

There are many ways and workarounds for this problem, this is not only going to be one solution but I am going to provide you with different options, as we try to solve this together.

The first option which you can try is to use different IDEs, like Visual code studio or Atom, Visual Code or some other IDE.

Another option is to use a Base model for your models by adding these lines to the beginning of your code.

                                                                       #
class BaseModel(models.Model):
    objects = models.Manager()
    class Meta:
        abstract = True
                                                                       #

You can then precede to use your models like you used to do before. In the example bellow we have an i number of models.

                                                                       #
class Model1(BaseModel):
    id = models.AutoField(primary_key=True)
...
class Model2(BaseModel):
    id = models.AutoField(primary_key=True)
.
.
.
.
...
class Modeli(BaseModel):
    id = models.AutoField(primary_key=True)
                                                                       #

This should solve the issue for most developers. I hope the fixes above fixed your problem.

Summing-up : 

Guys, this has been my best attempt at helping you understand and solve this issue. I hope you got rid of the error : Unresolved attribute reference objects for class ”…” I hope you found a solution which suits your needs. Consider helping the blog if you can by donating to our Kofi account.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/