开发者

Django import error [closed]

开发者 https://www.devze.com 2023-01-08 07:49 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a s开发者_开发技巧pecific moment in time,or an extraordinarily narrow situation that is not g
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a s开发者_开发技巧pecific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

I have two django-apps. news and comments in news/models.py I import my comments with "from my_proj.comments.models import Comment" and in my comments/models.py I import my news class with "from my_proj.news.models import News"

Then in my third app (named frontpage) I import News in the view.py. But I get the error: Could not import hb_frontpage.views. Error was: cannot import name News

If I delete the import in the comments/models.py file (and the functions that uses News) it works. Anyone know how to solve this?


You don't actually need the News import at all. Looking at your code (which by the way should have been posted as an update to your question, not as an answer) the only reference to News is to look up the objects that are related to this particular comment. But Django has a built-in way of doing that from the comment itself:

news = self.news_set.all()

Using this, there's no need to get the News object and filter from there.


You can't do circular imports.

News needs comments to load, but to load comments you need to load news, but to load news you need to load comments but to load comments...

You should really only need to do one import. If you write what you are trying to do, I can give further advice.

0

精彩评论

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