tags:

views:

29

answers:

3

Hi.

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?

A: 

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.

googletorp
A: 

Ah, I though that was the problem.

My news class:

some imports (including comments)

class News(models.Model):
  Some fields..
  ...
  ...
  comments = models.ManyToManyField(Comment,null=True, blank=True)

My comments class:

some imports (including news, that doesnt work..)

class Comment(models.Model):
  some fields.. like, author, text, created..
  ...

  def getURL(self):
    news = News.objects.all().filter(comments__id=self.id)
    # some_other_objects = some_other_objects.objects.all().filter(comments__id=self.id)

      if len(news) == 1:
        rtn = "/news/"+str(news[0].id)+"/"
        return rtn
      #if len(some_other_objeects) == 1:
        #rtn = "/some_other_object/"+str(some_other_objects[0].id+"/"
        #return rtn
    return None

I need to find out what class and object that is the parent to the comment.

origon
I solved it by adding some code in the view.py, the code does the smae thing as getURL.
origon
+1  A: 

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.

Daniel Roseman