开发者

Django import models

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 nam开发者_StackOverflow中文版ed 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜