1

OK -- I've been working with Django for a few months now and have come across a weird issue. To set it up, here's my webapp structure.

The main Django project is called cpm. I have a bunch of django apps in the cpm folder. Within each app, I have my models.py file.

Now, when I want to create/use models from other apps I would do something like:

from cpm.products.models import *

assuming an app named products was present. Recently, I started to get some errors saying things like, cannot import XYZ from products. So, after much searching, I changed the line:

from cpm.products.models import *

to

from products.models import *

I just dropped the cpm. part and now it works.

Can someone tell me why this is happening? It seems to be happening on only portions of my apps (I have a bunch within the CPM project). I want to make sure my syntax is accurate as I move forward.

Thanks!

1 Answer 1

5

The project root's directory got removed from the python path somewhere along the way, or you removed the __init__.py file from it's root.

On a side note, importing * will lead to issues, especially when you start adding lots of apps. Consider doing from products import models as prod_models. Then doing prod_models.MyModel where you need to reference your models.

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting. I created a new app tonight (./manage startapp) and notice that there is a file init.py in the directory that is not present in "older" app directories. Is this what you are referring to? Thanks for the tip on the importing of models. I'll be sure to refine my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.