Skip to content Skip to sidebar Skip to footer

Django Module 'appname' Has No Attribute 'models'

Everything was working fine until today after I deleted venv and re-created it with pycharm. Now, whatever command I run with django, It throws up this error: AttributeError: modul

Solution 1:

You need to add an __init__.py inside your root directory api_backend in order to treat this folder as a package (similar to the ones you have added in your other sub-folders/packages such as /migrations, /models, /serializers, etc.).

Edit: You can do relative import from the models folder like this inside your managers/field_data.py:

import os, sys
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
import models as api_models

Solution 2:

The problem here was the circular dependency errors. I solved this by restructuring my project. So now, managers was a sub-folder inside my models folder. My models can import managers, but managers cannot import models.

To achieve this, I referenced the model from the manager by using

self.model

and then, inside the model, I mention the managers like this:

classSampleModel(models.Model):
    objects = MyManager()
    ...

So to actually access the manager, I take the model and do

model.objects

this way, I dont need to access the managers directly out of the models folder. Hope this saves someone's time a lot.

what @Jarvis answered was also right, but at an overall view, restructuring my project was a better fix.

Post a Comment for "Django Module 'appname' Has No Attribute 'models'"